Kodewick
Lesson 3 of 30Contents

Output and input

Lesson 3 of 30 · Part 1 · Getting started · about 15 minutes

In lesson 2, %f always showed 6 decimal places: a price of 4.5 came out as 4.500000, and nobody writes prices like that. This lesson first shows how to make output neat, then how to make a program read numbers the user types.

main.c
1#include <stdio.h>2 3int main(void) {4    double price = 3.5;5    printf("%f\n", price);6    printf("%.2f\n", price);7    return 0;8}
Output
3.500000
3.50

Decimal places

Write .2 between the % and the f to show just two decimal places; .1 gives one, and .0 gives none. The digits left out are rounded:

main.c
1#include <stdio.h>2 3int main(void) {4    double temp = 36.678;5    printf("%.2f\n", temp);6    printf("%.1f\n", temp);7    printf("%.0f\n", temp);8    printf("%f\n", temp);9    return 0;10}
Output
36.68
36.7
37
36.678000

This only changes how the value is shown. The variable temp still holds 36.678, as the last line shows.

Width and alignment

A number right after the % is the width: the value takes up at least that many characters. If it is shorter, spaces are added in front, so the number lines up on the right. The square brackets are there so you can see the spaces:

main.c
1#include <stdio.h>2 3int main(void) {4    printf("[%5d]\n", 42);5    printf("[%5d]\n", 1234);6    printf("[%5d]\n", 123456);7    return 0;8}
Output
[   42]
[ 1234]
[123456]

123456 has 6 digits, more than the width, so it is shown in full; no digit is cut off. What happens with a minus sign in front of the width?

Guess first

What does this print?

1#include <stdio.h>2 3int main(void) {4    printf("[%-5d]\n", 42);5    return 0;6}

Width and decimal places work together. %8.2f means at least 8 characters with two decimal places. Line up a column of numbers this way and the decimal points sit one above another:

main.c
1#include <stdio.h>2 3int main(void) {4    printf("%-4d%8.2f\n", 1, 3.5);5    printf("%-4d%8.2f\n", 2, 12.25);6    printf("%-4d%8.2f\n", 3, 108.0);7    return 0;8}
Output
1       3.50
2      12.25
3     108.00
You writeMeaningShows
[%d] with 42as it is[42]
[%5d] with 42at least 5 characters, on the right[ 42]
[%-5d] with 42at least 5 characters, on the left[42 ]
[%.2f] with 3.14159two decimal places[3.14]
[%8.2f] with 3.14159at least 8 characters, two decimal places[ 3.14]

Printing % itself

In printf, % starts a format specifier. To show a real percent sign, write two of them: %%.

main.c
1#include <stdio.h>2 3int main(void) {4    int done = 75;5    double rate = 2.5;6    printf("%d%% done\n", done);7    printf("Interest rate: %.1f%%\n", rate);8    return 0;9}
Output
75% done
Interest rate: 2.5%

Reading a number with scanf

printf sends text out; scanf reads it in. It waits for the user to type something and puts the value it reads into a variable. Press Run and type a number. The page shows what happens when someone types 25 (what they type is not shown itself).

main.c
1#include <stdio.h>2 3int main(void) {4    int age;5    printf("How old are you?\n");6    scanf("%d", &age);7    printf("You are %d years old\n", age);8    return 0;9}
Output
How old are you?
You are 25 years old

In scanf("%d", &age), the %d says to read a whole number, and &age says to put it in age. Note the & in front of the variable name: scanf needs to know where the variable is in memory so it can put the value there. Lesson 15 explains why it works this way. For now, just remember: variables in scanf get a &, and variables in printf don't.

Tip: Forgetting the & is the most common scanf mistake. The compiler usually gives a warning, so read warnings too.

Reading several values

Just like printf, one scanf can have several format specifiers, matched in order with the variables after it:

main.c
1#include <stdio.h>2 3int main(void) {4    int width;5    int height;6    printf("Enter width and height:\n");7    scanf("%d %d", &width, &height);8    printf("Width %d, height %d\n", width, height);9    return 0;10}
Output
Enter width and height:
Width 3, height 4

The user can type 3 4 and press Enter, or 3, Enter, 4, Enter: %d skips any spaces and new lines before it and looks for the next number.

Reading decimals with %lf

To read a double, scanf needs %lf (the l stands for long), not %f. To print it, printf still uses %f:

main.c
1#include <stdio.h>2 3int main(void) {4    double price;5    printf("What is the price?\n");6    scanf("%lf", &price);7    printf("Price: %.2f\n", price);8    return 0;9}
Output
What is the price?
Price: 19.90
Typeprintf showsscanf reads
int%d%d
double%f%lf

What scanf returns

Users can type anything. When scanf is done, it gives back a number: how many values it read successfully. Store it in a variable and see, with a user who types abc:

main.c
1#include <stdio.h>2 3int main(void) {4    int count = 0;5    int n = scanf("%d", &count);6    printf("Read %d numbers\n", n);7    printf("count is still %d\n", count);8    return 0;9}
Output
Read 0 numbers
count is still 0

abc is not a whole number, so nothing was read: n is 0 and count wasn't changed. For now it's enough to know this exists. Once you learn if (lesson 5), you can use it to check whether the user really typed a number.

Practice

Write the code, then press Check. A hint costs nothing; the answer is there when you need it.

Exercise 1

Two decimal places

Make the program print Price: 3.50.

It should print
Price: 3.50
exercise1.c
1#include <stdio.h>2 3int main(void) {4    double price = 3.5;5    printf("Price: %f\n", price);6    return 0;7}
Output
Press Run or Ctrl+Enter
Exercise 2

Line them up

Make each of the three scores take 5 characters, aligned on the right, like below.

It should print
   95
    8
  100
exercise2.c
1#include <stdio.h>2 3int main(void) {4    int a = 95;5    int b = 8;6    int c = 100;7    printf("%d\n", a);8    printf("%d\n", b);9    printf("%d\n", c);10    return 0;11}
Output
Press Run or Ctrl+Enter
Exercise 3

Discount

Using one printf and these two variables, print 10% off, you pay 45.50.

It should print
10% off, you pay 45.50
exercise3.c
1#include <stdio.h>2 3int main(void) {4    int discount = 10;5    double total = 45.5;6 7    return 0;8}
Output
Press Run or Ctrl+Enter

What you learned

  • %.2f shows two decimal places, rounded; the value in the variable doesn't change.
  • %5d takes at least 5 characters, aligned right; %-5d aligns left; %8.2f combines width and decimal places.
  • To print %, write %%.
  • scanf("%d", &x) reads a whole number into x; the variable needs a & in front (lesson 15 explains why).
  • One scanf can read several values; read a double with %lf.
  • scanf returns how many values it read successfully.