Export (0) Print
Expand All
div
eof
Expand Minimize

printf Width Specification

In a format specification, the second optional field is the width specification. The width argument is a non-negative decimal integer that controls the minimum number of characters that are output. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values—depending on whether the left alignment flag (-) is specified—until the minimum width is reached. If width is prefixed by 0, leading zeros are added to integer or floating-point conversions until the minimum width is reached, except when conversion is to an infinity or NAN.

The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are output, subject to the precision specification.

If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value that's being formatted in the argument list, as shown in this example:

printf("%0*f", 5, 3); /* 00003 is output */

A missing or small width value in a format specification does not cause the truncation of an output value. If the result of a conversion is wider than the width value, the field expands to contain the conversion result.

Show:
© 2014 Microsoft