printf format flags
suggest changeFlags appear after % and before the field width:
| Flag | Meaning |
|---|---|
- |
left-justify within the field |
+ |
always show a sign for a signed conversion |
| space | prefix a space when a signed result has no sign |
# |
use the conversion's alternative form |
0 |
pad a numeric conversion with leading zeros when permitted |
For example:
printf("%+08d\n", 42); /* +0000042 */
printf("%#x\n", 42u); /* 0x2a */
printf("%-8s|\n", "C"); /* C | */
When + and the space flag both appear, the space flag is ignored. When - and 0 both appear, zero padding is ignored. For integer conversions, an explicitly specified precision also disables zero padding.
The exact effect of # depends on the conversion: it can add an octal or hexadecimal prefix, force a radix character for floating output, or retain trailing zeros for g and G.
Using a flag with a conversion for which the standard does not define it results in undefined behavior. POSIX and individual implementations provide extensions, such as locale-aware digit grouping; do not treat those as ISO C.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents