Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 printf() Documentation info: int printf(const char *format [,argument]...) Type of value returnedName of function First arg type and formal name (required,

Similar presentations


Presentation on theme: "1 printf() Documentation info: int printf(const char *format [,argument]...) Type of value returnedName of function First arg type and formal name (required,"— Presentation transcript:

1 1 printf() Documentation info: int printf(const char *format [,argument]...) Type of value returnedName of function First arg type and formal name (required, since no brackets) [ ] indicate optional arguments next argument type and name (in this case it may be any simple type) … indicates previous argument repeated zero or more times ( ) indicate printf is a function

2 2 printf() int printf(const char *format [,argument]...) Type of value returned ( int in this case) All functions return at most one value. The type void is used to indicate a function returns no value There is no requirement to use the value returned. The printf() function returns the number of characters printed (including spaces); returns negative value if error occurs.

3 3 printf() int printf(const char *format [,argument]...) Name of function; printf( ) in this case A function name is ALWAYS followed by a set of (), even if the function takes no arguments

4 4 printf() int printf(const char *format [,argument]...) Type ( const char * ) and name ( format ) of first argument For the moment, const char * can be thought of as a series of characters enclosed in double quotes The name format may be thought of as a code indicating how the arguments are to be interpreted, and how the output should look.

5 5 printf() int printf(const char *format [,argument]...) zero of more optional arguments, each preceded by a comma zero because of the … optional because of the [ ]

6 6 examples - printf() float a=67.49,b= ; printf("hello %f there %f\n",a,b); printf("%f%f%f%f\n",a,a,b,b); printf("a=%f, b=%f",a,b); printf("Cool huh?\n"); Printed: hello there a= , b= Cool huh? Note: default %f prints as many digits as needed to represent the whole part of the value and exactly 6 digits to the right of the decimal point.

7 7 examples - printf() float a=67.49,b= ; printf("Price:█%10f█%10f\n",a,b); printf("%8.2f█%8.4f█%5.1f█%7.5f\n",a,a,b,b); printf("a=%5.5f,█b=%0.2f\n",a,b); Printed: print position Price:██ ███ columns ███67.49██ ██10.0█ to 35 a= ,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width.

8 8 examples - printf() float a=67.49,b= ; int i=184,j=-51; double x= , y=-22.33; printf("%7.1lf%5d%8.2f\n",y,i,a); printf("%13.2f%4d%9.2lf\n",b,j,x); //changed Printed: print position columns to 35 Notes: d indicates decimal int eger f indicates float lf (that’s lower case L and f) indicates double (for double output, f OK too; not OK for input)

9 9 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type flagmeaningdefault -Left align the result within the given field widthRight align +Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-) 0If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. No padding (actually space padding) blankPrefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears #When used with the o, x, or X format, the # flag prefixes any nonzero output with 0, 0x, or 0X, respectively. Ignored when used with c, d, i, u, or s. No prefix #When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Decimal point appears only if digits follow it. #When used with g or G format, forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Decimal point appears only if digits follow it. Trailing zeros are truncated. This slide adapted from information in MSDN Library

10 10 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. Notes: If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). This slide adapted from information in MSDN Library

11 11 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type typmeaningdefault cThe precision has no effectcharacter is printed d,i, u,o, x,X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision Default precision is 1. e,EThe precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default precision is 6; if precision is 0 or the period fThe precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. (.) appears without a number following it, no decimal point is printed. g,GThe precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s,SThe precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered. This slide adapted from information in MSDN Library

12 12 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type to specifyuse prefixwith type specifier long, long intld, i, o, x, X long unsigned intlu short inthd, i, o, x, X short unsigned inthu __int64I64d, i, o, x, X Note: this is not a complete list. Note: these prefixes are Microsoft extensions and not ANSI compatible This slide adapted from information in MSDN Library

13 13 printf format specifications General form:% [flags] [width] [.precision] [ { h | l | I64 | L } ] type typewhat cSingle byte char d or iSigned decimal integer X or xUnsigned hexadecimal integer (using ABCDEF or abcdef) oUnsigned octal integer uUnsigned decimal integer fSigned real value having the form [-]xxxx.yyyy where yyyy is dependant upon the requested precision, and xxxx is dependent upon the requested width and/or magnitude of value e or E Signed real value of form [-]xxxx.yyyye ± zzz, where xxxx and yyyy are as described for f; zzz is a three digit exponent. E is the same as e except a capital E is printed g or GAutomatically selects f or e/E based on the magnitude of the value sString - assumes start address given; terminates at \0 (null) Note: not a complete list This slide adapted from information in MSDN Library

14 14 printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78);..../ / / /<= ruler vv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78);..../ / / /<= ruler vv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78);..../ / / /<= ruler vv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78);..../ / / /<= ruler vv 12ww34 xx +56yy 78zz

15 15 printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78);..../ / / /<= ruler v-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78);..../ / / /<= ruler v 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78);..../ / / /<= ruler v -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78);..../ / / /<= ruler v 012w 034x056 y +078z

16 16 printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6);..../ / / /<= ruler w -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6);..../ / / /<= ruler w 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n", , );..../ / / /<= ruler w x z


Download ppt "1 printf() Documentation info: int printf(const char *format [,argument]...) Type of value returnedName of function First arg type and formal name (required,"

Similar presentations


Ads by Google