Input and Output (printf, scanf)
How your C program talks to the user — reading input and showing output.
printf() — Displaying Output
printf() prints formatted text to the screen using format specifiers: %d for integers, %f for floats, %c for characters, %s for strings. You can combine text and variables in one statement.
int age = 20;
printf("Age: %d years\n", age);scanf() — Reading Input
scanf() reads user input into a variable. You must pass the ADDRESS of the variable using &, so scanf knows where in memory to store the value.
int age;
scanf("%d", &age); // note the &Common Mistakes
Forgetting the & before a variable in scanf() (except for strings/arrays, which already act like addresses) is the single most common beginner mistake, causing crashes or garbage values.
🌍 Real-World Use
Every command-line tool you use (like a simple login prompt or a calculator app) is built on this same input-output pattern — display a prompt with printf, then read the response with scanf.
💡 Pro Tip
Always leave a space before %d in scanf when reading multiple values in a row (like "%d %d") — this tells scanf to skip any leftover whitespace/newline characters from the previous input.
🧪 Quick Self-Test
Check what you just learned — no pressure, just practice.
1. Why does scanf() need & before a variable?
2. Which format specifier is used for a float in printf?