Show contents
Chapter 3 of 4 · 20 min
Variables and types
By the end of this chapter, you'll be able to
- Declare and initialise variables of type int, double and char
- Display a variable with printf and the formats %d, %f, %.2f and %c
- Change a variable by assignment
- Predict the result of an integer division and of the modulo %
Declaring a variable
A variable is a box in memory with a name. In C, you declare it by giving its type first, then its name:
#include <stdio.h>
int main(void) {
int notebooks = 3; // a whole number
double price = 1.25; // a decimal number
char initial = 'K'; // a single character, between single quotes
printf("%d notebooks at %.2f euros\n", notebooks, price);
printf("Total: %.2f euros\n", notebooks * price);
printf("Initial: %c\n", initial);
return 0;
}
3 notebooks at 1.25 euros
Total: 3.75 euros
Initial: K
The three types you'll use all the time:
| Type | Holds | Example | printf format |
|---|---|---|---|
int | a whole number (positive or negative) | 42, -7 | %d |
double | a decimal number | 3.14, -0.5 | %f (6 decimals), %.2f (2 decimals) |
char | a character | 'A', '7', '?' | %c |
In the text given to printf, each format (%d, %.2f…) is a placeholder. It's replaced by the next value in the list, in order. The number of formats and the number of values must match, and each type must have its own format.
C uses a dot for decimals, never a comma: 1.25.
Assigning means replacing
The = sign is assignment: it computes the value on the right, then stores it in the variable on the left, overwriting the old value.
printf replaces %d with the value of balance.
Text description of the animation
- This snippet is inside main. Memory is empty for now.
- We reserve an int box called balance and store 1000 in it.
- A second box, withdrawal, receives 250.
- We first compute the right-hand side with the old value of balance, then store 750 in balance.
- withdrawal is replaced by 100: the old value 250 is lost.
- Same principle: balance is now 650.
- printf replaces %d with the value of balance.
Very common shortcuts: balance -= 100; is the same as balance = balance - 100;, and counter++; adds 1 to counter.
A variable declared without a value (int x;) contains anything at all: whatever was lying around in memory. Get into the habit of always initialising your variables when you declare them.
Calculations and integer division
The operators are +, -, * and /, plus % for the remainder of the division (the modulo). The trap is division: between two ints, the result is an int, and the decimal part is simply cut off.
#include <stdio.h>
int main(void) {
int minutes = 135;
printf("%d h %d min\n", minutes / 60, minutes % 60);
printf("7 / 2 = %d\n", 7 / 2);
printf("7.0 / 2 = %.1f\n", 7.0 / 2);
return 0;
}
2 h 15 min
7 / 2 = 3
7.0 / 2 = 3.5
As soon as at least one of the two numbers is a double (7.0), the calculation keeps the decimals.
Your turn
Worked exercise
Worked exercise: converting a duration
A video lasts 4000 seconds. Write a program that stores this duration in an int variable, then displays 1 h 6 min 40 s.
Hint 1
seconds / 3600 gives the number of whole hours.Hint 2
seconds % 3600. Then do it again with 60 for the minutes.Show the solution and common mistakes
#include <stdio.h>
int main(void) {
int seconds = 4000;
int hours = seconds / 3600;
int rest = seconds % 3600;
int minutes = rest / 60;
int secs = rest % 60;
printf("%d h %d min %d s\n", hours, minutes, secs);
return 0;
}
1 h 6 min 40 s
Common mistakes:
- Computing the minutes with
seconds / 60: you get 66, because you forget to take the hours away. - Using
doubles:4000.0 / 3600gives 1.11…, and you'd then have to truncate the result. Here, integers are exactly the right tool.
Summary
- 1A C variable has a type fixed when it's declared: int, double or char.
- 2printf uses one format per value: %d for int, %f or %.2f for double, %c for char.
- 3= stores the value on the right in the variable on the left and overwrites the old one.
- 4Between two ints, / cuts off the decimal part and % gives the remainder.
- 5Always initialise your variables and keep -Wall to catch incorrect formats.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.