C in 100 Seconds: Format Specifiers | Episode 3
Video: C in 100 Seconds: Same Data Different Output — Format Specifiers | Episode 3 by Taught by Celeste AI - AI Coding Coach
Watch full page →Format Specifiers — Controlling Output in C
C in 100 Seconds, Episode 3
printf doesn't just print values — it formats them. Format specifiers give you precise control over how numbers, strings, and addresses appear on screen.
The Basics
int n = 42;
float pi = 3.14159;
char name[] = "Alice";
Each type has a matching specifier:
printf("%d\n", n); // 42 — decimal integer
printf("%x\n", n); // 2a — hexadecimal
printf("%f\n", pi); // 3.141590 — float (6 decimals default)
printf("%.2f\n", pi); // 3.14 — float (2 decimals)
printf("%s\n", name); // Alice — string
printf("%p\n", &n); // 0x7ff... — memory address
Width and Alignment
You can control column width with a number between % and the specifier:
printf("[%10d]\n", n); // [ 42] — right-aligned, 10 wide
printf("[%-10d]\n", n); // [42 ] — left-aligned, 10 wide
The - flag flips the alignment. This is how you build aligned tables and formatted reports in C.
Why Does This Matter?
Format specifiers are not optional in C — they're the only way to convert data to text. Higher-level languages handle this automatically. In C, you're telling printf exactly how to interpret each argument's bits.
Get the specifier wrong (like using %d for a float) and you won't get an error — you'll get silent, incorrect output.
Full Code
#include <stdio.h>
int main() {
int n = 42;
float pi = 3.14159;
char name[] = "Alice";
printf("%d\n", n);
printf("%x\n", n);
printf("%f\n", pi);
printf("%.2f\n", pi);
printf("%s\n", name);
printf("%p\n", &n);
printf("[%10d]\n", n);
printf("[%-10d]\n", n);
return 0;
}
Compile and Run
gcc format.c -o format
./format
Next episode: Arithmetic Operators — doing math in C (and why integer division might surprise you).
Student code: github.com/GoCelesteAI/c-in-100-seconds/episode03