Format specifications always begin with a percent sign (%) and are read left to right. Itconsists of optional and required fields, has the following form:
flags] [width] [.precision] type
Optional fields are shown with '[field name]. The brackets are not part of the specification and simply indicate that the field is optional. There are 20+ "types" and their detailed specifications can be found in a 'C' or 'C++'
Here are several sample format codes for a integer number whch is "field type" 'd'.
%d |
Format routines will calculate the size of the formatted item. |
%6d |
Item will be 6 characters wide. Right justified and padded with blanks on the left. The 6 characters includes '-' for negative numbers. A '+' sign characters will not be displayed for positive numbers. |
%06d | Same as above but padded with zeroes on the left. |
%+6d |
Same as '%6d' except a sign character of either '+' or '-' will always be included. |
Several sample format codes for a floating point number whch is "field type" 'f'. The number is displayed as [ – ]dddd.dddd, where dddd is one or more decimal digits.
%f |
Format routines will calculate the size of the formatted item. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. |
%6f |
Item will be 6 characters wide. Right justified and padded with blanks on the left. |
%6.3f |
Item will be 6 characters wide. There will be three fractional digits after the decimal point. |
Here are several sample format codes for a floating point number whch is "field type" 'f'. The number is displayed as [ – ]dddd.dddd, where dddd is one or more decimal digits.
%10c |
Takes precisely 10 characters. Padded with blanks on the right. |
%-10c |
Takes precisely 10 characters. Padded with blanks on the left. |
Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.