printf conversion specifiers
suggest changeThe conversion specifier determines how an argument is formatted. The argument's actual promoted type must match the format. A mismatch causes undefined behavior.
| Specifier | Argument type | Output |
|---|---|---|
d, i |
int |
signed decimal |
u |
unsigned int |
unsigned decimal |
o |
unsigned int |
octal |
x, X |
unsigned int |
hexadecimal |
f, F |
double |
fixed-point |
e, E |
double |
scientific notation |
g, G |
double |
shorter of fixed or scientific notation |
a, A |
double |
hexadecimal floating-point |
c |
int |
one character after conversion to unsigned char |
s |
pointer to char |
characters up to the terminating null character or precision limit |
p |
void * |
implementation-defined pointer representation |
n |
int * |
stores the number of characters written so far |
% |
none | a literal percent sign |
float arguments are promoted to double, and narrow integer types are promoted to int or unsigned int when passed through ....
Length modifiers change the required type. For example, %ld expects long, %zu expects size_t, and %Lf expects long double. They also modify the pointer type required by %n.
For %s, a precision limits the maximum number of bytes read from the array:
printf("%.8s", text);
This can bound an output operation but does not prove that an arbitrary pointer is valid for that many bytes.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents