Presentation is loading. Please wait.

Presentation is loading. Please wait.

printf() Documentation info:

Similar presentations


Presentation on theme: "printf() Documentation info:"— Presentation transcript:

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

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 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 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 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 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 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 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 integer f indicates float lf (that’s lower case L and f) indicates double (for double output, f OK too; not OK for input)

9 printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type flag meaning default - Left align the result within the given field width Right 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 (-) If 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) blank Prefix 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 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 printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type typ meaning default c The precision has no effect character 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,E The 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 f The 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,G The precision specifies the maximum number of significant digits printed. Six significant digits are printed, with any trailing zeros truncated. s,S The 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 printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type to specify use prefix with type specifier long, long int l d, i, o, x, X long unsigned int u short int h short unsigned int __int64 I64 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 printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type type what c Single byte char d or i Signed decimal integer X or x Unsigned hexadecimal integer (using ABCDEF or abcdef) o Unsigned octal integer u Unsigned decimal integer f Signed 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 G Automatically selects f or e/E based on the magnitude of the value s String - assumes start address given; terminates at \0 (null) Note: not a complete list This slide adapted from information in MSDN Library

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 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 w x y z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../ / / / <= ruler v w x y z

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


Download ppt "printf() Documentation info:"

Similar presentations


Ads by Google