BETA Try the new lesson view
Code

The student card

Create the folder practice/02 with two files in it, as in topic 1.

practice/02/CMakeLists.txt: the program p02_card from main.c:

add_executable(p02_card main.c)

practice/02/main.c: a student card. The program declares variables, works out a third value from two of them and prints everything:

  • const int current_year = 2026; is the current year: it doesn't change in the program.
  • int study_year, char group, int birth_year, double average: each with its own type and starting value.
  • int age = current_year - birth_year; shows that a starting value can be an expression too. It is worked out at the moment the program reaches this line.
  • In the printf lines every variable gets its own format: %d, %c, %.2f.
  • sizeof study_year is how many bytes the variable takes. The result is printed with %zu.

The last printf doesn't fit on one line, so its arguments continue on the next one. C doesn't mind: a statement ends only at the ;.

Run it

File → Reload CMake Project, pick p02_card and click ▶:

Year of study: 1
Group: PI25A
Age: 19 years
Average: 8.75
In memory: int 4 B, double 8 B, char 1 B

Change birth_year to your own year of birth and run it again: the age changes too, because it is calculated.

Works? Commit: Add student card.