Learn/C/3. Reading input and arithmetic
Lesson 3 of 15
Sign in

C, lesson 3

Reading input and arithmetic

Programs get more interesting when they react to data. In C you read from standard input (the keyboard, or text piped into the program) with scanf, the mirror image of printf.

The format string says what to expect: %d reads an int, %lf reads a double (note the l, which differs from printf), %lld reads a long long, and " %c" reads one character. The in front of the variable passes its , so knows where to store the value. Forgetting the is the classic beginner bug: it compiles with a warning and then crashes or prints garbage.

2 exercises to try what you just read.

Exercise 1

Fahrenheit to Celsius

Press Ctrl Enter or Check to test your code.

Exercise 2

Split the bill

Press Ctrl Enter or Check to test your code.

&
address
scanf
&

scanf skips spaces and new lines before a number, so the same code reads 3 4 on one line or 3 and 4 on two lines:

scanf returns how many values it managed to store. You will use that in later lessons to detect bad input or the end of the data.

Arithmetic operators

OperatorMeaningint exampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23
%remainder7 % 21

Multiplication, division and remainder happen before addition and subtraction, just like in maths. Use parentheses whenever the order is not obvious.

Integer division

When both sides of / are integers, C throws away the fractional part (it truncates toward zero). 7 / 2 is 3, and -7 / 2 is -3. The % operator gives what was thrown away: 7 % 2 is 1. Together they are perfect for splitting things into units:

If you want a fractional answer, make at least one side a double:

(double) is a cast: it converts the value to another type for this expression. Dividing an integer by zero is undefined behaviour and usually crashes the program, so check for zero before you divide.

Shorthand assignment

C has compact forms for updating a variable in place:

Mixing types

In an expression with an int and a double, the int is converted to double first, so 3 * 0.5 is 1.5. Going the other way, assigning a double to an int drops the fraction: int n = 2.9; stores 2. The compiler will not stop you, so be deliberate about which type each variable has.

Further reading

  • cppreference: scanf, fscanf, sscanf
  • cppreference: Arithmetic operators
  • cppreference: Cast operator

Read one temperature in degrees Fahrenheit (it may have a fractional part) and print it in Celsius with one digit after the point, using C = (F - 32) * 5 / 9.

Input: one number. Output: the Celsius value followed by C.

A group shares a restaurant bill. Read two integers: the total in cents and the number of people (at least 1). Everyone pays the same whole number of cents, rounded down, and the leftover cents go in the tip jar.

Print two lines: what each person pays in dollars with exactly two digits for the cents, and how many cents are left over.

Hint: / and % give the share and the leftover. Then / 100 and % 100 split the share into dollars and cents, and %02d prints a number with at least two digits, padding with a zero.

int age;
scanf("%d", &age);
printf("Next year you will be %d.\n", age + 1);
int a, b;
scanf("%d %d", &a, &b);
int total_minutes = 135;
int hours = total_minutes / 60;     // 2
int minutes = total_minutes % 60;   // 15
int slices = 7, people = 2;
double each = (double)slices / people;   // 3.5
int points = 10;
points += 5;    // points = points + 5  -> 15
points *= 2;    // 30
points -= 1;    // 29
points++;       // 30, add one
points--;       // 29, subtract one
Input:
212
Output:
100.0 C
Input:
1003 3
Output:
Each pays $3.34
Tip jar: 1 cents