printf
Formatters — Quick ReferenceA single-page cheat sheet showing the common format specifiers, length modifiers, flags, width/precision, and examples for printf()
in C and C++. (Now includes an interactive simulator!)
Specifier | Meaning | Example |
---|---|---|
%d / %i | Signed decimal integer | printf("%d", 42); |
%u | Unsigned decimal integer | printf("%u", 42); |
%o | Unsigned octal | printf("%o", 42); |
%x / %X | Unsigned hexadecimal (lower/upper) | printf("%x", 42); |
%f / %F | Decimal floating point | printf("%.2f", 3.14); |
%e / %E | Scientific notation | printf("%e", 3.14); |
%g / %G | Shortest of %e or %f | printf("%g", 3.14); |
%a / %A | Hexadecimal floating point | printf("%a", 3.14); |
%c | Character | printf("%c", 'A'); |
%s | String | printf("%s", "Hello"); |
%p | Pointer | printf("%p", ptr); |
%n | Number of characters so far (writes to int*) | printf("%n", &x); |
%% | Literal percent sign | printf("%%"); |