printf() Formatting Flags, Width & Precision
%[flags][width][.precision][length]specifier
The standard output function printf() writes formatted output to stdout:
| Format | Example | Output |
|---|---|---|
%-8d | printf("%-8d!", 42); | "42 !" (Left-aligned) |
%+d | printf("%+d", 42); | "+42" (Explicit sign) |
%05d | printf("%05d", 42); | "00042" (Zero padded) |
%.2f | printf("%.2f", 3.14159); | "3.14" (2 decimal precision) |
%#x | printf("%#x", 255); | "0xff" (Hex with prefix) |
%p | printf("%p", (void*)&x); | "0x7ffee2" (Memory Address) |