2. printf Function
int printf (control string, arg1, arg2, …, argn)
• Writes the control string to the standard
output.
• If control string includes format specifier
(subsequences beginning with %), the
additional arguments following control
string are formatted and inserted in the
resulting string replacing their respective
specifiers.
4. Format Specifier of printf
%[flags][width][.precision][length]specifier
Specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 07
x or X Unsigned hexadecimal integer 7fa or 7FA
f Floating point 392.65
e Scientific notation 3.9265e+2
g Use the shortest representation: %e or %f 392.65
c character A
s String of characters sample
% % followed by percent write % to the output %
6. Format Specifier of printf
%[flags][width][.precision][length]specifier
Specifiers
length d , i u , o , x, X f , e ,g c s
none int unsigned int float char char *
hh signed char unsigned char
h short int unsigned short int
l long int unsigned long int double(lf)
ll long long int unsigned long long int
L 392.65 long double
8. Format Specifier of printf
%[flags][width][.precision][length]specifier
width description
number Minimum number of characters to be printed. If the value to be printed
is shorter than this number, the result is padded with blank spaces. The
value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional
integer value argument preceding the argument that has to be
formatted.
10. Format Specifier of printf
%[flags][width][.precision][length]specifier
.precision description
.number For e, f , g specifiers: this is the no of digits to be printed after the
decimal point (by default this is 6)
For s : this is the maximum number of characters to be printed. By
default all the characters are printed until the ending null character is
encountered.
.* The precision is not specified in the format string, but as an additional
integer value argument preceding the argument that has to be
formatted.
12. Format Specifier of printf
%[flags][width][.precision][length]specifier
flags Description
- Left-justify within the given field width; right justification is the default
+ Forces to precede the result with a plus or a minus sign.
# For o, x, X : the value is preceded with 0, 0x, 0X respectively for values
different than zero.
For e, f, g : it forces the written output to contain a decimal point even
if no digits follow.
0 Left-pads the number with zeros instead of spaces when padding is
specified
14. Practice problems
1. A C program contains the following variable declarations
float a=2.5, b=0.0005, c=3000.;
Show the output resulting from each of the following printf
statements.
a) printf(“%f %f %f”,a,b,c);
b) printf(“%3f %3f %3f”,a,b,c);
c) printf(“%8f %8f %8f”,a,b,c);
d) printf(“%8.4f %8.4f %8.4f”,a,b,c);
e) printf(“%8.3f %8.3f %8.3f”,a,b,c);
f) printf(“%-8f %-8f %-8f”,a,b,c);
g) printf(“%+8f %+8f %+8f”,a,b,c);
h) printf(“%08f %08f %08f”,a,b,c);
i) printf(“%#8f %#8f %#8f”,a,b,c);
15. Practice problems
2. Assume that i ,j , k are integer variables and i represents an
octal quantity, j represents a decimal quantity and k
represents a hexadecimal quantity. Write an appropriate
printf function for each of the following statements:
a) Display the values for i ,j , k with minimum field width of
eight characters for each value
b) Repeat part (a) with each output data item left justified
within its respective field.
c) Repeat part (a) with each output data item preceded by
zeros(0x, in the case of the hexadecimal quantity)