Flag Directives

The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.

Flag Characters

Flag

Meaning

Default

Left align the result within the given field width.

Right align.

+

Prefix the output value with a sign (+ or –) if the output value is of a signed type.

Sign appears only for negative signed values (–).

0

If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) and a precision specification is also present (for example, %04.d), the 0 is ignored.

No padding.

blank (' ')

Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear.

No blank appears.

#

When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.

No blank appears.

 

When used with the e, E, f, a or A format, the # flag forces the output value to contain a decimal point in all cases.

Decimal point appears only if digits follow it.

 

When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.

Ignored when used with c, d, i, u, or s.

Decimal point appears only if digits follow it. Trailing zeros are truncated.

See Also

Reference

Precision Specification