Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

160 StandardI/O Library Chapter 5


specifications, other characters in the format arecopied unmodified. Aconversion
specification has four optional components, shown in squarebrackets below:
%[flags][fldwidth][precision][lenmodifier]convtype
The flags aresummarized in Figure5.7.

Flag Description
’ (apostrophe) format integer with thousands grouping characters


  • left-justify the output in the field



  • always display sign of a signed conversion
    (space) prefix by a space if no sign is generated

    convert using alternative form (include 0x prefix for hexadecimal format, for example)


    0 prefix with leading zeros instead of padding with spaces




Figure 5.7The flags component of a conversion specification

Thefldwidthcomponent specifies a minimum field width for the conversion. If
the conversion results in fewer characters, it is padded with spaces. The field width is a
non-negative decimal integer or an asterisk.
Theprecisioncomponent specifies the minimum number of digits to appear for
integer conversions, the minimum number of digits to appear to the right of the decimal
point for floating-point conversions, or the maximum number of bytes for string
conversions. The precision is a period (.) followed by a optional non-negative decimal
integer or an asterisk.
Either the field width or precision (or both) can be an asterisk. In this case, an
integer argument specifies the value to be used. The argument appears directly before
the argument to be converted.
Thelenmodifiercomponent specifies the size of the argument. Possible values
aresummarized in Figure5.8.

Length modifier Description
hh signed or unsignedchar
h signed or unsignedshort
l signed or unsignedlongor wide character
ll signed or unsignedlong long
jintmax_toruintmax_t
zsize_t
tptrdiff_t
Llong double

Figure 5.8The length modifier component of a conversion specification

Theconvtype component is not optional. It controls how the argument is
interpreted. The various conversion types aresummarized in Figure5.9.
With the normal conversion specification, conversions areapplied to the arguments
in the order they appear after the format argument. An alternative conversion
specification syntax allows the arguments to be named explicitly with the sequence%n$
Free download pdf