Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

printf format specifiers

The C printf format specifier table: conversions, flags, width, precision and length modifiers, with the right specifier for every standard type.

Every printf-family function reads a format string that contains conversion specifications. Each one starts with % and describes how to turn the next argument into text. The general shape is:

TEXT
%[flags][width][.precision][length]conversion

Only the % and the conversion character are required. This page is the lookup table; the functions themselves are described in printf, fprintf and snprintf. The scanf family uses similar but not identical specifiers (see scanf).

Try it

C The conversions side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Flags, width, precision and length modifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Conversions

Specifier Argument type Output
%d, %i int Signed decimal
%u unsigned int Unsigned decimal
%o unsigned int Octal
%x, %X unsigned int Hexadecimal, lowercase or uppercase digits
%f, %F double Fixed point (inf/nan in uppercase with F)
%e, %E double Scientific notation (1.234568e+03)
%g, %G double Shorter of %f and %e; trailing zeros removed
%a, %A double Hexadecimal floating point (0x1p+0)
%c int One character (the value converted to unsigned char)
%s const char * Characters up to the null terminator (or the precision)
%p void * Pointer, implementation-defined text
%n int * Stores the count of characters written so far; nothing is printed
%% none A literal percent sign

Flags

Flag Effect
- Left-justify within the width (default is right-justify)
+ Always print a sign for signed numbers
space Put a space before positive numbers (ignored if + is used)
0 Pad with zeros instead of spaces (ignored with -, and for integers when a precision is given)
# Alternate form: 0 prefix for %o, 0x for %x (non-zero values), always a decimal point for floats

Width and precision

  • Width is a minimum field size. A number such as 8 or a * that takes the width from an int argument placed before the value. A negative * width means the - flag plus the absolute value.
  • Precision starts with a dot. For integers it is the minimum number of digits, for %f/%e the digits after the decimal point (default 6), for %g the significant digits, and for %s the maximum number of characters to print. .* takes it from an int argument. A bare . means 0.
  • A value wider than the width is never truncated.

Length modifiers

Modifier Integer conversions (d i u o x X) Notes
hh signed char / unsigned char Argument is promoted, then converted back
h short / unsigned short
(none) int / unsigned int
l long / unsigned long Also %lc (wint_t) and %ls (wchar_t *)
ll long long / unsigned long long
j intmax_t / uintmax_t
z size_t / the signed counterpart Use %zu for size_t
t ptrdiff_t and its unsigned counterpart Use %td
L long double with f e g a %Lf

For %f, %e and %g the argument is always double: a float is promoted, and %lf is the same as %f in printf (unlike scanf). Fixed-width types use the macros of <inttypes.h>: PRId32, PRIu64, PRIx8 and so on, spliced into the format by adjacent string literal concatenation.

Usage notes

  • A mismatch is undefined behavior. %d with a long, %f with an int, or %s with an int can print garbage or crash. Compile with -Wall -Wformat and treat warnings as errors.
  • %c takes an int, so printf("%c", 65) prints A. To print a number, use %d.
  • %s needs a terminated string. A null pointer is undefined (glibc often prints (null), but do not rely on it). A precision limits the read, so %.*s with a length is safe for non-terminated data.
  • Rounding. %.2f rounds the exact binary value; printf("%.1f", 0.25) prints 0.2 on a typical system because of round-half-to-even on exact ties.
  • Too few arguments or extra ones. Missing arguments are undefined; extra arguments are ignored.
  • C23 additions. %wN/%wfN length modifiers for _BitInt and fixed-width integers exist in C23 libraries; %b (binary) is standardized in C23 but glibc 2.28, used by our runner, does not support it. Print binary digits with a loop instead.
  • Printing bool. It is promoted to int, so %d shows 0 or 1; use b ? "true" : "false" for words.