Loading
0x40Lesson 5 of 15

Input and output

Format output precisely with printf, and read what the user types with scanf.

12 min 5-question quiz 2 code exercises
By the end of this lesson you can
  • Format numbers with width and precision in printf
  • Read numbers from the user with scanf and &
  • 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 |

receipt.c
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}
Output
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.)

double_it.c
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:

  • scanf returns how many values it successfully read. If the user types abc when you asked for %d, it returns 0 and leaves number untouched. Check it.
  • For double, scanf uses %lf (not %f, which is for float). This is one of the few places printf and scanf differ.

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

  • printf specifiers must match the value types: %d int, %f double, %c char, %s string. Width and precision go between % and the letter (%6.2f).

  • scanf needs the address of each variable (&x), and uses %lf for double.

  • scanf returns 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.

Exercise 1

Add two numbers

+25 XP

Read two integers from input and print their sum in this format:

3 + 4 = 7

The input is two numbers separated by a space.

  • 3 and 4
  • -10 and 25
main.c
Loading editor…

Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.

Exercise 2

Temperature converter

+25 XP

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
main.c
Loading editor…

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…

Did you like the lesson? 😆👍
Consider a donation to support our work: