printf("I am %d years old and %.1f meters tall.\n", 21, 5.9);
// ^^ placeholder for int ^^^ placeholder for floatC Format Specifiers: %d, %f, %s and the Complete Reference
What is C Format Specifiers? Core Concept & Direct Summary
What is the complete reference of C format specifiers for integers, floating points, characters, strings, pointers, and fixed-width types?
If C I/O functions were a language, format specifiers would be its alphabet. Those little two-character codes — %d, %f, %s — appear in nearly every printf() and scanf() you'll ever write, and picking the wrong one is behind a shocking number of beginner bugs.
Here's the good news: there are only about a dozen you actually need to know, and each one follows the same simple logic. This page is the complete reference — every specifier that matters, what it works with, how it behaves differently in printf() versus scanf(), and the mistakes to dodge. Bookmark this one; you'll be coming back to it.
(Suggested image: a colorful "cheat sheet" style graphic of the specifier table — this page's hero image and your most shareable asset in the whole series)
How Does C Format Specifiers Work? In-Depth Mechanics & Architecture
What Is a Format Specifier, Exactly?
A format specifier is a placeholder inside a format string. It starts with % and ends with a type character, and it tells the function two things:
- In printf(): "Insert a value here, and format it as this type"
- In scanf(): "Expect this type of input, and store it in the matching variable"
Each specifier grabs its value from the argument list in order. First % pairs with the first argument, second % with the second, and so on.
The Big Five: Specifiers You'll Use Every Day
Let's go deep on the five specifiers that cover 90% of real-world code.
%d — Signed Integer
The workhorse of C programming. Reads or prints whole numbers:
(%i does the same job in printf(). In scanf() it differs slightly — %i auto-detects octal/hex prefixes while %d always assumes decimal — but for everyday use, treat them as twins.)
%f — Floating Point
For decimal numbers:
The default six decimal places surprises everyone at first. Tame it with precision: %.2f for money, %.1f for measurements.
%s — String
Prints or reads a whole string (character array):
Two things to burn into memory: scanf("%s") stops at whitespace (use fgets() for full lines), and always give it a width limit (%49s for a 50-char array) to prevent buffer overflow. And no & — array names already act as addresses.
%c — Single Character
That leading space in scanf(" %c") is crucial — without it, %c grabs leftover whitespace from previous input. We covered this buffer behavior in detail in the scanf() tutorial.
%x — Hexadecimal
Prints integers in base-16 — essential for debugging, memory work, and embedded programming:
The Complete Reference Table
Here's the full list, organized by category:
Integers
Floating Point
Characters & Strings
Special
printf vs scanf: Where the Same Specifiers Behave Differently
This is the section most references skip — and where most bugs live. The specifier codes look identical in both functions, but the rules differ in subtle, important ways:
Difference 1: %f vs %lf
Why? In printf(), floats get automatically promoted to doubles when passed as arguments, so %f works for both. But scanf() receives addresses, not values — it needs to know the exact size of the variable it's writing into. %f = 4-byte float, %lf = 8-byte double. Mix them up and scanf() writes 8 bytes into a 4-byte home. No error. Just silent corruption.
Difference 2: The & operator
printf reads values; scanf writes to addresses. The only exception: strings, which never take & in either function.
Difference 3: Whitespace handling
Numbers skip whitespace by default; characters don't. Asymmetry = bugs.
(Suggested image: a two-column comparison graphic — "printf side vs scanf side" listing these three differences)
What Happens When You Use the Wrong Specifier?
Format mismatches cause undefined behavior — C-speak for "anything can happen, and none of it is good":
The %s mismatch is the dangerous one: it treats your value as a memory address and starts reading whatever lives there. The integer/float mixups usually "just" print garbage — the bit pattern of a float reinterpreted as an integer, which is why the numbers look so bizarre.
Protect yourself: compile with warnings enabled:
With -Wformat, GCC checks your format strings at compile time and catches mismatches before they bite. Turn it on, always.
The %g Trick Nobody Teaches
Tired of 3.140000? %g automatically chooses between %f and %e — whichever is shorter — and strips trailing zeros:
For general-purpose number printing where you don't need fixed decimals, %g gives the cleanest output. Criminally underused.
Width and Precision: The Quick Reminder
Every specifier accepts the full decoration pattern we covered in the formatted I/O tutorial:
The pattern is always %[flags][width][.precision][length]type — and it works identically across every specifier in the table.
A Test-Yourself Example
Before checking the output, try to predict what each line prints:
Output:
Line 3 is the one worth a second look: the same variable printed as %c shows the character, while %d shows its ASCII value. One variable, two views — that's the whole idea of format specifiers in a nutshell.
The Ten Commandments of Format Specifiers
- Match the specifier to the variable type — always, no exceptions
- Double in scanf needs %lf; in printf, %f handles both
&in scanf, never in printf; strings never need&at all- Limit string input:
scanf("%49s", arr)forchar arr[50] - Space before %c in scanf to skip leftover newlines
%.2ffor money,%gfor clean general output- %% prints a literal % — no argument needed
- %zu for sizeof results — not %d
- Compile with -Wall -Wformat so the compiler checks your specifiers
- Never use %n — it's a security hole, not a feature
(Suggested image: a styled "Ten Commandments" graphic — this is prime Pinterest/LinkedIn content and reinforces the reference-page identity)
Final Thoughts
Format specifiers look like trivia, but they're really a contract: you tell C exactly what type of data is crossing the boundary between your program and the outside world, and C trusts you completely. Keep your side of the contract — right specifier, right type, right size — and I/O bugs practically vanish.
You don't need to memorize the whole table today. Know the big five cold (%d, %f, %s, %c, %x), remember the %lf scanf rule, and bookmark this page for everything else. That's how everyone actually works — including people who've written C for decades.
Runnable C Code Example & Practical Implementation
#include <stdio.h>
int main() {
int integer = 100;
double pi = 3.14159265;
char letter = 'C';
char title[] = "Tutorial";
void *addr = &integer;
size_t size = sizeof(double);
printf("Integer: %d | Unsigned: %u\
", integer,(unsigned)integer);
printf("Float: %.4f | Scientific: %.2e\
", pi, pi);
printf("Char: %c | String: %s\
", letter, title);
printf("Pointer: %p | size_t: %zu bytes\
", addr, size);
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
(void*) when printing with %p (printf("%p", (void*)ptr)) to comply with ANSI/ISO C standards.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: Why is `%lf` required in `scanf` for double, but `%f` is used in `printf` for double?
Q: What specifier portably formats a `size_t` variable?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.