printf format specifiers

printf format specifiers

printf specifiers control conversions, width, precision, and layout. Match them to promoted argument types; precision, not width, limits string output.

CategoryCheat sheets
KindCheat sheets
Header<stdio.h>
C standardC99
Category Form Argument type or effect Example
Conversion specifier %d / %i Signed int; both produce decimal output, and %i is identical to %d in printf. %d, %i
Conversion specifier %u unsigned int; writes unsigned decimal. %u
Conversion specifier %o unsigned int; writes octal without a prefix. %o
Conversion specifier %x / %X unsigned int; writes hexadecimal with lowercase or uppercase letters. %x, %X
Conversion specifier %f double; writes fixed-point notation. A float variadic argument is promoted to double. %.2f
Conversion specifier %F double (C99); similar to %f for fixed-point output, but uses uppercase forms such as INF, INFINITY, and NAN for infinities and NaNs. %F
Conversion specifier %a / %A double (C99); writes hexadecimal floating-point notation, using 0x/0X for the significand prefix and p/P for the binary exponent marker. %a, %A
Conversion specifier %e / %E double; writes scientific notation with e or E as the exponent marker. %.3e, %.3E
Conversion specifier %g / %G double; chooses an f- or e-style representation according to value and precision, removing redundant trailing zeros by default. %.6g, %.6G
Conversion specifier %c int; the value is converted to unsigned char and written as one character. %c
Conversion specifier %s char *; points to a null-terminated character array and writes characters before the terminator. %s
Conversion specifier %p void *; writes a pointer value in an implementation-defined format. %p
Conversion specifier %n int *; stores the number of characters written so far through the pointer and writes no output itself. %n
Conversion specifier %% Consumes no argument; writes a literal percent sign. %%
Length modifier h %hd / %hu The argument is first integer-promoted; its value is converted to short / unsigned short before output. %hd, %hu
Length modifier hh (C99) %hhd / %hhu The argument is first integer-promoted; its value is converted to signed char / unsigned char before output. %hhd, %hhu
Length modifier l %ld / %lu Accepts long int / unsigned long int respectively; l also applies to other eligible integer conversions. %ld, %lu
Length modifier ll (C99) %lld / %llu Accepts long long int / unsigned long long int respectively. %lld, %llu
Length modifier j (C99) %jd / %ju Accepts intmax_t / uintmax_t respectively; the types are defined in stdint.h, and inttypes.h also provides corresponding format macros. %jd, %ju
Length modifier t (C99) %td / %tu %td accepts ptrdiff_t; %tu accepts the unsigned integer type corresponding to ptrdiff_t. ptrdiff_t is defined in stddef.h. %td, %tu
Length modifier z (C99) %zu Use %zu for size_t and %zd for the signed integer type corresponding to size_t; these conversions require those exact argument types. %zu
Length modifier L %Lf Accepts long double; uppercase L also works with e, E, g, and G. %Lf
Minimum field width %5d Uses a minimum field width of 5 and right-aligns by default; wider values are not truncated. %5d
Dynamic width * %*d Consumes an int for width before the value; a negative width requests left alignment. %*d
Precision %.2f For f conversion, writes 2 digits after the decimal point; the exact meaning of precision depends on the conversion. %.2f
Precision %.3s For s conversion, writes at most 3 characters without requiring a null terminator there. %.3s
Flag - %-10s Left-aligns within a field of width 10 and pads on the right with spaces. %-10s
Flag + %+d Always writes a sign for signed values, including + for positive values. %+d
Space flag % d Prepends a space to a signed value without a minus sign; + overrides the space flag. % d
Flag # %#x Uses alternative form: nonzero hexadecimal values receive 0x, while %#o ensures a leading 0 for octal. %#x
Flag 0 %08.2f Pads with zeros to the requested width; this example has total width 8 and two fractional digits. %08.2f

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

integer:    42
hex: 0xff
fixed: 12.50
left: |C         |
long: 123456
percent: 100%
⚠️ Warning A mismatch between a format and its promoted argument type causes undefined behavior. In particular, do not transfer scanf's %lf rule to printf: a float variadic argument to printf has already been promoted to double.
⚠️ Warning %n writes through a caller-provided address. Never use user input directly as a format string; an untrusted format can cause unintended writes, type mismatches, or disclosure of sensitive data.
💡 Tip Both numeric width and * specify a minimum field width, not a maximum length. Use precision, such as %.20s, to limit string output.

📚 Related Tutorials

❓ FAQ

QWhat is the difference between %f and %lf in printf, and is scanf the same?
AIn C99 printf, l has no effect on f, so both %f and %lf expect double. scanf differs: %f requires float *, whereas %lf requires double *; they are not interchangeable.
QHow should size_t be printed portably in C89 and C99?
AUse %zu in C99. C89 has no z modifier; explicitly convert the value to unsigned long and use %lu, as in printf("%lu", (unsigned long)sizeof value).
QHow do I pass arguments for * width and precision in printf?
AWhen * replaces a width or precision, printf reads the corresponding int from the variadic arguments before the value being formatted. Argument order must match the asterisks and other specifiers. A negative width acts like its positive value with left alignment; a negative precision is treated as omitted.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏