C, lesson 3
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.
&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.
| Operator | Meaning | int example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3 |
% | remainder | 7 % 2 | 1 |
Multiplication, division and remainder happen before addition and subtraction, just like in maths. Use parentheses whenever the order is not obvious.
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.
C has compact forms for updating a variable in place:
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.
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; // 15int slices = 7, people = 2;
double each = (double)slices / people; // 3.5int points = 10;
points += 5; // points = points + 5 -> 15
points *= 2; // 30
points -= 1; // 29
points++; // 30, add one
points--; // 29, subtract oneInput:
212
Output:
100.0 CInput:
1003 3
Output:
Each pays $3.34
Tip jar: 1 cents