Input & output: printf, scanf & format specifiers
12 min lesson
- Input and output (I/O) is how a program talks to the user: printing messages on screen and reading values the user types.
- I/O matters because without it a program cannot show results or receive data, which makes it nearly useless.
- Think of printf as a waiter writing your order on a notepad and showing it to you, and scanf as the waiter listening to what you say and writing it down.
- printf prints formatted text to the screen. The first argument is a format string with placeholders like %d, and later arguments fill them in.
- scanf reads typed input and stores it in variables. It needs the address of each variable using the & operator, like scanf("%d", &age).
- A format specifier is the code after % that tells printf or scanf what type to expect: %d for int, %f for float, %lf for double, %c for char, %s for strings.
- The computer takes each placeholder, matches it with the next argument, converts the value to text (for printf) or text to a value (for scanf), and writes it out or stores it.
- The syntax is: printf("label %d\n", value); and scanf("%d", &variable);.
- A realistic use: asking the user for their age and then printing it back to confirm.
- A surprising detail: forgetting the & in scanf usually crashes the program, because scanf writes to a wrong memory address.
- The example program reads two numbers from a fixed string using sscanf, then prints their sum. scanf works the same way but reads from the keyboard.
- Match every format specifier to the correct type or you will get garbage or crashes.
- Always put & before variables in scanf (except for strings and arrays).
- Use \n in printf to end lines cleanly and keep output readable.
- For double use %lf in scanf but %f in printf is fine too on modern systems; be consistent.
- Practice by printing a few values with different specifiers. You are learning a very practical skill!
#include <stdio.h>
int main(void) {
// Two variables ready to receive the numbers we read
int firstNumber = 0;
int secondNumber = 0;
// A fixed string that plays the role of typed input
// (scanf would read the same text from the keyboard)
const char *inputText = "12 30";
// Read two integers from the string into our variables
// %d matches a whole number, and & gives scanf the address
int matched = sscanf(inputText, "%d %d", &firstNumber, &secondNumber);
// Check that both numbers were found before using them
if (matched == 2) {
// Print the two numbers with labels
printf("First: %d\n", firstNumber);
printf("Second: %d\n", secondNumber);
// Print their sum
printf("Sum: %d\n", firstNumber + secondNumber);
} else {
// Tell the user something went wrong
printf("Could not read two numbers.\n");
}
return 0;
}What happens: First: 12 Second: 30 Sum: 42
Breaking it down
Variables for input
int firstNumber = 0;Creates a box to hold the first number. It is initialized to 0 so it is never uninitialized if the read fails.
Fixed input string
const char *inputText = "12 30";Plays the role of what the user would type. Using a fixed string keeps the program deterministic so its output is always the same.
sscanf call
sscanf(inputText, "%d %d", &firstNumber, &secondNumber)Reads two integers from the string. The & gives the address so sscanf knows where to store each value. It returns how many it matched.
Checking the result
if (matched == 2)Confirms that both numbers were found before using them. Without this check the variables might still be 0 or garbage.
printf with %d
printf("Sum: %d\n", firstNumber + secondNumber);Computes the sum and prints it where %d is. The \n moves to a new line so output stays tidy.
Common format specifiers
| Specifier | Type | printf example | scanf example |
|---|---|---|---|
| %d | int | printf("%d", 42) | scanf("%d", &n) |
| %f | float | printf("%f", 3.14f) | scanf("%f", &f) |
| %lf | double | printf("%lf", 3.14) | scanf("%lf", &d) |
| %c | char | printf("%c", 'A') | scanf("%c", &c) |
| %s | string | printf("%s", "hi") | scanf("%s", name) |
| %u | unsigned int | printf("%u", 42u) | scanf("%u", &u) |
| %zu | size_t | printf("%zu", sizeof(int)) | scanf("%zu", &z) |
A helpful analogy
printf is like a waiter writing your order on a notepad and reading it back to you. The format string is the notepad with blanks, and the placeholders like %d are the blanks filled in with your actual order. scanf is the waiter listening to you and writing down what you say into the right boxes.
Nesting rules
- 1.The number and order of placeholders must match the number and order of arguments.
- 2.Every scanf argument that is not a string or array must have & before it.
- 3.Use %lf for double in scanf; using %f there is a common bug that corrupts memory.
- 4.Use \n at the end of printf lines to keep output readable; forgetting it makes everything run together.
Common mistakes
Forgetting & in scanf
Avoid
scanf("%d", age); // crashesUse instead
scanf("%d", &age);scanf needs the address of the variable, not its value. Without & it writes to a random place in memory and usually crashes.
Using the wrong specifier
Avoid
printf("%d", 3.14); // garbageUse instead
printf("%f", 3.14);Each specifier tells printf how to interpret the bits. Using %d on a floating-point value prints nonsense.
Using %f for double in scanf
Avoid
double d;
scanf("%f", &d); // corruptsUse instead
double d;
scanf("%lf", &d);%f expects a float pointer, not a double pointer. The mismatch writes the wrong number of bytes and corrupts memory.
Quick tips
- •Always check what scanf returns; it tells you how many items were read successfully.
- •Add a space before %c in scanf if you previously read a number, to skip leftover newline characters.
- •Use %.2f in printf to print two decimal places neatly.
- •Try printing the same number with %d, %f and %c to see how the specifier changes the output.
- •Keep the format string and the arguments on one line when possible, so mismatches are easier to spot.