C, lesson 2
A variable is a named box in memory that holds a value. In C every box has a type, and you must say the type when you create the variable. The type tells the compiler how many bytes to set aside and how to interpret them.
The line int lives = 3; is a declaration with an initializer. You can also declare first
and assign later, but a variable you declare without a value holds garbage until you set it,
so give every variable a starting value.
| Type | Holds | Example | printf format |
|---|---|---|---|
int | whole numbers, at least 16 bits (32 on almost every modern machine) | -42 | %d |
2 exercises to try what you just read.
long long | big whole numbers, at least 64 bits | 9000000000LL | %lld |
double | floating-point numbers, about 15 significant digits | 3.14 | %f, %.2f |
char | one character (really a small integer) | 'x' | %c |
bool | true or false | true | %d (prints 1 or 0) |
The format specifier inside printf must match the type of the value you pass. If it does not,
the output is wrong or the behaviour is undefined, and the compiler usually warns you.
%.2f means "print a double with two digits after the point". A number between % and the
letter sets a minimum width: %5d pads the number with spaces to five characters.
Names may contain letters, digits and underscores, and must not start with a digit. They are
case sensitive: total and Total are different variables. C programmers usually write
snake_case for variables and functions. Pick names that say what the value means
(seconds_left, not s).
If a value should never change, say so. const makes the compiler reject any later
assignment:
C23 adds constexpr, which goes one step further: the value must be known at compile time, so
you can use it anywhere C needs a true constant, such as the size of an array.
Older C code uses #define DAYS_PER_WEEK 7 for the same job. That is a text substitution done
before compiling and has no type, so prefer constexpr in new code.
C23 also lets you write auto and have the compiler infer the type from the initializer:
Use it sparingly. In C, seeing the type written out is usually clearer.
Whole-number types have a fixed range. Going past the top of a signed type such as int is
undefined behaviour: the program may print nonsense or do something stranger. When numbers
can get large (populations, money in cents, file sizes), use long long.
The starter declares the stats of a game character. Print them as a card in exactly this format:
Use the variables, with the right format specifier for each type. Health is shown with one digit after the point. The program reads no input.
Declare these constexpr int constants: seconds_per_minute (60), minutes_per_hour (60),
hours_per_day (24) and days_per_week (7). Use them to compute the number of seconds in a
week and in a 365-day year, then print exactly:
The program reads no input.
int lives = 3; // a whole number
double price = 4.99; // a number with a fractional part
char grade = 'B'; // a single character, in single quotes
bool done = false; // true or false (a keyword since C23)int score = 0;
score = score + 10; // the right side is computed first, so score is now 10printf("%d lives, $%.2f, grade %c\n", lives, price, grade);
// 3 lives, $4.99, grade Bconst double tax_rate = 0.2;
// tax_rate = 0.25; // error: assignment of read-only variableconstexpr int days_per_week = 7;
int hours[days_per_week]; // fine: the size is a compile-time constantauto count = 12; // int
auto ratio = 0.5; // doubleName: Ada
Level: 7
Health: 82.5
Grade: A
Alive: 1A week has 604800 seconds.
A year has 31536000 seconds.