Input and output
Format output precisely with printf, and read what the user types with scanf.
- Format numbers with width and precision in
printf - Read numbers from the user with
scanfand& - Check that input was actually read
Programs get interesting when they respond to input. C's standard library gives you printf to write formatted text and scanf to read it, both from <stdio.h>.
printf format specifiers
printf takes a format string followed by values. Each % specifier in the format is replaced by the next value, so the types must match:
| Specifier | For | Example output |
| --- | --- | --- |
| %d | int | 42 |
| %f | double | 3.140000 |
| %.2f | double, 2 decimals | 3.14 |
| %c | char | A |
| %s | string | hello |
| %5d | int, padded to width 5 | 42 |
| %-5d | int, left-aligned in width 5 | 42 |
| %05d | int, zero-padded | 00042 |
1#include <stdio.h>
2
3int main(void) {
4 printf("%-8s%6s\n", "Item", "Price");
5 printf("%-8s%6.2f\n", "Coffee", 3.5);
6 printf("%-8s%6.2f\n", "Bagel", 2.25);
7 return 0;
8}Item Price Coffee 3.50 Bagel 2.25
Reading input with scanf
scanf uses the same kind of format string, but it writes into your variables. To let it do that, you pass each variable's address with &. (You will learn exactly what addresses are in the pointers lesson; for now, remember: scanf needs & in front of int, double and char variables.)
1#include <stdio.h>
2
3int main(void) {
4 int number;
5 printf("Enter a number: ");
6 if (scanf("%d", &number) != 1) {
7 printf("That was not a number.\n");
8 return 1;
9 }
10 printf("Twice that is %d\n", number * 2);
11 return 0;
12}Two details worth copying into every program you write:
scanfreturns how many values it successfully read. If the user typesabcwhen you asked for%d, it returns 0 and leavesnumberuntouched. Check it.- For
double,scanfuses%lf(not%f, which is forfloat). This is one of the few placesprintfandscanfdiffer.
Reading several values
scanf("%d %d", &a, &b) reads two integers separated by any whitespace - spaces or newlines. In the exercises on this page, the input is provided for you (like a file being piped into your program), so you can focus on the logic.
Key takeaways
printfspecifiers must match the value types:%dint,%fdouble,%cchar,%sstring. Width and precision go between%and the letter (%6.2f).scanfneeds the address of each variable (&x), and uses%lffordouble.scanfreturns how many values it read - check it.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write real C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Add two numbers
Read two integers from input and print their sum in this format:
3 + 4 = 7The input is two numbers separated by a space.
- 3 and 4
- -10 and 25
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Temperature converter
Read a temperature in Celsius (a decimal number) and print it in Fahrenheit with one decimal place. F = C × 9 / 5 + 32.
21.5C = 70.7F- 21.5 C
- -40 C
- 100 C
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Sign in to ask questions, help other learners, and earn XP for taking part.
Loading posts…