07-printf-scanf-spec-v2

advertisement
printf()
Documentation info:
int printf(const char *format [,argument] ...)
1
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.
2
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
3
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.
4
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 [ ]
5
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.999925
67.49900067.4990009.9999259.999925
a=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.
6
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
12345678901234567890123456789012345
Price:██67.499000███9.999925
███67.49██67.4900██10.0█9.99993
a=67.49000,█b=10.00
print
position
columns
1 to 35
Note: On last line of output, the actual value was output; the
printf routine overrode the specified width.
7
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
12345678901234567890123456789012345
-22.3 184
67.49
10.00 -51
-22.33
print
position
columns
1 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)
8
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type
flag meaning
default
-
Right align
Left align the result within the given field width
+
Prefix the output value with a sign (+ or -) if the output value is of a Sign appears only for
signed type.
negative signed values (-)
0
If width is prefixed with 0, zeros are added until the minimum width No padding (actually space
is reached. If 0 and - appear, the 0 is ignored.
padding)
blankPrefix the output value with a blank if the output value is signed and No blank appears
positive; the blank is ignored if both the blank and + flags appear.
#
When used with the o, x, or X format, the # flag prefixes any
No prefix
nonzero output with 0, 0x, or 0X, respectively. Ignored when used
with c, d, i, u, or s.
#
When used with the e, E, or f format, the # flag forces the output
Decimal point appears only
value to contain a decimal point in all cases.
if digits follow it.
#
When used with g or G format, forces the output value to contain a Decimal point appears only
decimal point in all cases and prevents the truncation of trailing
if digits follow it. Trailing
zeros.
zeros are truncated.
This slide adapted from information in MSDN Library
9
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).
10
This slide adapted from information in MSDN Library
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type
typ meaning
default
c
character is printed
The precision has no effect
d,i, The precision specifies the minimum number of digits to be printed.
u,o, If the number of digits in the argument is less than precision, the
x,X output value is padded on the left with zeros. The value is not
truncated when the number of digits exceeds precision
e,E The precision specifies the number of digits to be printed after the
decimal point. The last printed digit is rounded.
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.
g,GThe precision specifies the maximum number of significant digits
printed.
s,S The precision specifies the maximum number of characters to be
printed. Characters in excess of precision are not printed.
Default precision is 1.
Default precision is 6; if
precision is 0 or the period
(.) appears without a
number following it, no
decimal point is printed.
Six significant digits are
printed, with any trailing
zeros truncated.
Characters are printed until
a null character is
encountered.
11
This slide adapted from information in MSDN Library
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type
to specify
long, long int
long unsigned int
short int
use prefix
l
l
h
with type specifier
d, i, o, x, X
u
d, i, o, x, X
short unsigned int
__int64
h
I64
u
d, i, o, x, X
Note: this is not a complete list.
Note: these prefixes are Microsoft extensions and not
ANSI compatible
12
This slide adapted from information in MSDN Library
printf format specifications
General form: % [flags] [width] [.precision] [ { h | l | I64 | L } ] type
type
c
d or i
X or x
o
u
f
what
Single byte char
Signed decimal integer
Unsigned hexadecimal integer (using ABCDEF or abcdef)
Unsigned octal integer
Unsigned decimal integer
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
13
This slide adapted from information in MSDN Library
printf() examples
printf("vv% dww%-dxx%+dyy%dzz\n",-12,-34,-56,-78);
..../....1..../....2..../....3..../ <= ruler
vv-12ww-34xx-56yy-78zz
printf("vv% dww%-dxx%+dyy%dzz\n",12,34,56,78);
..../....1..../....2..../....3..../ <= ruler
vv 12ww34xx+56yy78zz
printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",-12,-34,-56,-78);
..../....1..../....2..../....3..../ <= ruler
vv -12ww-34 xx -56yy -78zz
printf("vv% 5dww%-5dxx%+5dyy%5dzz\n",12,34,56,78);
..../....1..../....2..../....3..../ <= ruler
vv
12ww34
xx +56yy
78zz
14
printf() examples
printf("v% 05dw%-05dx%+05dy%05dz\n",-12,-34,-56,-78);
..../....1..../....2..../....3..../ <= ruler
v-0012w-34 x-0056y-0078z
printf("v% 05dw%-05dx%+05dy%05dz\n",12,34,56,78);
..../....1..../....2..../....3..../ <= ruler
v 0012w34
x+0056y00078z
printf("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n",-12,-34,-56,-78);
..../....1..../....2..../....3..../ <= ruler
v
-012w
-034x-056
y
-078z
print("v%7.3dw% 7.3dx%-7.3dy%+7.3dz\n", 12, 34, 56, 78);
..../....1..../....2..../....3..../ <= ruler
v
012w
034x056
y
+078z
15
printf() examples
printf("w%7.2fx%7.2fy%7.2fz\n",-1.234,-3.456,-5.6);
..../....1..../....2..../....3..../ <= ruler
w -1.23x -3.46y -5.60z
printf("w%7.2fx%7.2fy%7.2fz\n", 1.234, 3.456, 5.6);
..../....1..../....2..../....3..../ <= ruler
w
1.23w
3.46y
5.60z
printf("w%7.2fx%7.2f\n",-1234567.8,1234567.8);
..../....1..../....2..../....3..../ <= ruler
w-1234567.8x1234567.8z
16
Download