Format Specifiers Reference in C
Every % conversion has the shape %[flags][width][.precision][length]specifier: %-10s left-aligns in width 10, %08.2f zero-pads to two decimals, %zu prints size_t, %lld prints long long. The length modifiers (h, l, ll, z, t) adapt integer specifiers to short/long/long-long/size_t/ptrdiff_t — using %d for size_t is undefined behavior on 64-bit platforms.
Two families to memorize cold: %d %u %x %f %c %s %p %zu, and float behavior (printf promotes float to double, so %f covers both; scanf needs %f for float* but %lf for double* — the classic asymmetry).
Core eight
%d int, %u unsigned, %x hex, %f double, %c char, %s string, %p pointer, %zu size_t — the daily vocabulary.
Width, precision, flags
%10d pads to width 10, %.2f fixes two decimals, %-10s left-aligns, %08d zero-pads — compose freely.
Length modifiers
%hd short, %ld long, %lld long long, %zd ssize_t — match the argument's true type or risk UB.
The scanf asymmetry
printf %f takes float AND double (promotion); scanf %f takes float* while double* needs %lf — mixing them corrupts memory.
#include <stdio.h> int main(void) { printf("|%10s|\n", "hi"); printf("|%-10s|\n", "hi"); printf("%.2f\n", 3.14159); printf("%zu\n", sizeof(int)); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.