180 likes | 522 Views
printf(). Documentation info: int printf( const char *format [,argument] ...). Type of value returned. Name of function. First arg type and formal name (required, since no brackets). ( ) indicate printf is a function. next argument type and name (in this case it may be any simple type).
E N D
printf() Documentation info: int printf(const char *format [,argument] ...) Type of value returned Name of function First arg type and formal name(required, since no brackets) ( ) indicate printf is a function next argument type and name(in this case it may be any simple type) … indicates previous argument repeated zero or more times [ ] indicate optional arguments
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.
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
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.
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 [ ]
examples - printf() float a=67.49,b=9.999925;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 67.490000 there 9.99992567.49900067.4990009.9999259.999925a=67.490000, b=9.999925Cool 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.
examples - printf() float a=67.49,b=9.999925;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: 00000000011111111112222222222333333 print12345678901234567890123456789012345 positionPrice:██67.499000███9.999925 columns███67.49██67.4900██10.0█9.99993 1 to 35a=67.49000,█b=10.00 Note: On last line of output, the actual value was output; the printf routine overrode the specified width.
examples - printf() float a=67.49,b=9.999925;int i=184,j=-51;double x=123.456, 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: 00000000011111111112222222222333333 print12345678901234567890123456789012345 position -22.3 184 67.49 columns 10.00 -51 -22.33 1 to 35 Notes: d indicates decimal integerf indicates floatlf (that’s lower case L and f) indicates double(for double output, f OK too; not OK for input)
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type This slide adapted from information in MSDN Library
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
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type This slide adapted from information in MSDN Library
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type 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
printf format specifications General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type Note: not a complete list This slide adapted from information in MSDN Library
printf() examples printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv-12ww-34xx-56yy-78zz printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34xx+56yy78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulervv -12ww-34 xx -56yy -78zz printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulervv 12ww34 xx +56yy 78zz
printf() examples printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv-0012w-34 x-0056y-0078z printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78); ..../....1..../....2..../....3..../ <= rulerv 0012w34 x+0056y00078z printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78); ..../....1..../....2..../....3..../ <= rulerv -012w -034x-056 y -078z print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78); ..../....1..../....2..../....3..../ <= rulerv 012w 034x056 y +078z
printf() examples printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6); ..../....1..../....2..../....3..../ <= rulerw -1.23x -3.46y -5.60z printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6); ..../....1..../....2..../....3..../ <= rulerw 1.23w 3.46y 5.60z printf("w%7.2fx%7.2f\n",-1234567.8,1234567.8); ..../....1..../....2..../....3..../ <= rulerw-1234567.8x1234567.8z