350 Chapter 16 Input and Output Operations in C
Ta b le 16.4 printfConversion Characters
Char Use to Display
ior d Integer
u Unsigned integer
o Octal integer
x Hexadecimal integer, using a–f
X Hexadecimal integer, using A–F
forF Floating-point number, to six decimal places by default
eor E Floating-point number in exponential format (eplaces lowercase e before
the exponent,Eplaces uppercase E before exponent)
g Floating-point number in for eformat
G Floating-point number in For Eformat
aor A Floating-point number in the hexadecimal format 0xd.ddddp±d
c Single character
s Null-terminated character string
p Pointer
n Doesn’t print anything; stores the number of characters written so far by this
call inside the intpointed to by the corresponding argument (see note from
Ta b le 16.3)
% Percent sign
Ta b les 16.1 to 16.4 might appear a bit overwhelming. As you can see, many different
combinations can be used to precisely control the format of your output.The best way
to become familiar with the various possibilities is through experimentation. Just make
certain that the number of arguments you give to the printffunction matches the
number of %signs in the format string (with %%as the exception, of course). And, in the
case of using an *in place of an integer for the field width or precision modifiers,
remember that printfis expecting an argument for each asterisk as well.
Program 16.1 shows some of the formatting possibilities using printf.
Program 16.1 Illustrating the printfFormats
// Program to illustrate various printf formats
#include <stdio.h>
int main (void)
{
char c = 'X';
char s[] = "abcdefghijklmnopqrstuvwxyz";
int i = 425;
short int j = 17;
unsigned int u = 0xf179U;
long int l = 75000L;