Learn C
#include "ioformatting/format_specifiers.h"

Format Specifiers Reference in C

#concept

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).

#how it works
1

Core eight

%d int, %u unsigned, %x hex, %f double, %c char, %s string, %p pointer, %zu size_t — the daily vocabulary.

2

Width, precision, flags

%10d pads to width 10, %.2f fixes two decimals, %-10s left-aligns, %08d zero-pads — compose freely.

3

Length modifiers

%hd short, %ld long, %lld long long, %zd ssize_t — match the argument's true type or risk UB.

4

The scanf asymmetry

printf %f takes float AND double (promotion); scanf %f takes float* while double* needs %lf — mixing them corrupts memory.

#example
#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;
}
Output
| hi|
|hi |
3.14
4
1%10s right-aligns hi in a 10-wide field (default for strings is actually left? no — right, as shown).
2%-10s flips to left alignment — the minus flag controls the side.
3%.2f rounds to two decimals and %zu prints the size_t 4 portably.
#use cases
Aligned tables and receipts with width/flag combinations
Money output locked to two decimals via %.2f
Portable size printing with %zu instead of %d/%lu guesses
Getting scanf float-vs-double specifiers right the first time

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.