c Any value converted to unsigned char and displayed; c is used mainly for printing characters.

s The argument is converted to a character array and is printed; the last null in the string is not printed.

f Floating point.

e, E Floating point displayed in exponential form. It will have one digit to the left of the decimal point; the number of digits on the right side of the decimal point depends on the required precision.

g, G The value can be printed in floating point or exponential form. The exponential form is used if the exponent is less than –1 or if the exponent causes more places than required by the specified precision; the decimal point appears only if it is followed by a digit.

n This indicates to print the number of characters that are printed so far by printf.

p It indicates an additional argument pointer to void; the value of the pointer is converted to a sequence of characters.

Type prefixes

h It can appear before type indicators d, i, o, u, x, and X. It indicates that the value to be displayed should be interpreted as short; for example, short integer (hd) and short unsigned integer (hu).

l It can appear before type-identifiers d, i, o, u, x, and X. It indicates that the value to be displayed should be interpreted as long; for example, long integer (hd) and long unsigned integer (hu).

l, L Available for type-identifiers e, E, f, g, and G. It indicates that a value should be indicated as long double.

Field-width

Field-width indicates the least number of columns that will be allocated to the output. For example, if you write %4d to i and the value of i is 10, then 4 columns are allocated for i and 2 blank are added on left side of value of i. So the output is bb10. Here, b indicates blank.

If the value is more than the specified column, field-width is ignored and the number of columns used is equal to the number of columns required by the arguments. So if i is 12345 then 5 columns are used, even if %4d is specified.

In any circumstance, the output width is not shortened, because of field-width.

If you specify * instead of field-width then you have to specify additional arguments. For example,

printf ("%*d\n", 5, 20); // Aprintf ("%*d\n", 20, 5); // B

In A, 5 is substituted for * and it indicates putting the value 20 in 5 columns.

In B, 20 is substituted for * and it indicates putting the value 5 in 20 columns.

Precision

Precision indicates the minimum number of digits printed for type integers d, i, o, u, x, and X. For example,

i. printf("%10.4d\n", 35)

Here 10 is the field-width and 4 is the precision, so 10 columns are used for the 4-digit output. To make 35 into 4 digits, two 0s are added to the left side to make it 0035. To print 0035 in 10 columns, blanks are added to make the output bbbbbb0035.

For floating arguments, precision indicates how many digits are printed after decimal points. If precision is more than the number of digits on the right side of the decimal point, 0s are added to the right side.

If precision indicates too few digits, then it is ignored and the number of digits are printed as necessary.

Flags

Flag characters are used to give directives for the output. You can use multiple flag characters in any order.

The flag characters are as follows:

— Indicates that output is left justified.

printf("%-10.4d\n", 25)

It causes the number to be printed as 0025bbbbbb. Thus, blanks are added to the right side.

In the absence of a flag, it is printed as bbbbbb0025.

+ Indicates that i number is printed using a sign character (+ or —).

printf("%+d\n", -25);printf("%+d\n", 25);

It causes printing as

-25+25

Indicates a space for positive values so that positive values and negative values are aligned. For example,

printf("% d\n", 25);printf("% d", 25);

It causes printing in the form of

b2525

In the first case, blank is displayed.

− # Indicates that the value should be converted to another form before displaying. For example, for hexadecimal values you can indicate 0X; for the floating data type, # indicates that the decimal point should always be included in the output.

0 Used with whole and real numbers, 0 causes 0s to be padded to complete the field width. If the precision is specified as 0, then this flag is ignored; if the 0 and – flags are both specified, the 0 flag is ignored.

ESCAPE SEQUENCE

Escape sequences are the special directives used to format printing. For example, \n indicates that the next printing should start from the first column of the next line. Following are the escape sequences: