You are looking at lesson 2. Your place is still lesson 1, task 1: Algorithm.
The formats %d, %lld, %f, %.2f, %c, %zu, width for even columns, and why a wrong format is an error.
The first argument of printf is the format string. Each % in it is replaced by the next argument, in order:
printf("Age: %d years, average: %.2f\n", age, average);
The first %d gets age, %.2f gets average. There must be as many formats as arguments, and each one's type must fit:
| Format | Type | Example | Prints |
|---|---|---|---|
%d |
int |
printf("%d", 42) |
42 |
%lld |
long long |
printf("%lld", 3153600000LL) |
3153600000 |
%f |
double |
printf("%f", 8.75) |
8.750000 |
%.2f |
double |
printf("%.2f", 8.333) |
8.33 |
%c |
char |
printf("%c", 'A') |
A |
%zu |
the result of sizeof |
printf("%zu", sizeof(int)) |
4 |
%% |
– | printf("100%%") |
100% |
%f always shows 6 digits after the dot. %.2f shows 2 and rounds: 8.336 would become 8.34.
A number between % and the letter sets the width: how many characters the value takes. The missing room is filled with spaces.
printf("[%5d]\n", 42);
printf("[%-5d]\n", 42);
printf("[%8.2f]\n", 3.14159);
[ 42]
[42 ]
[ 3.14]
%5d aligns to the right, %-5d to the left, and %8.2f takes 8 characters in all, 2 of them after the dot. This way table columns line up even when the numbers have different lengths. In topic 1 you used \t for that.
If the format doesn't fit the argument's type, printf prints garbage. The course flags (-Wall and -Werror) turn such a place into a compile error:
main.c:5:23: error: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Werror=format=]
5 | printf("Average: %d\n", average);
| ~^ ~~~~~~~
| | |
| int double
| %f
GCC shows both types (int and double) and suggests the right format, %f. The same happens when you try to print a long long with %d: GCC suggests %lld.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.