You are looking at lesson 7. Your place is still lesson 10, task 1: struct.
Declaration and definition, how a program is built (preprocessor, compiler, linker), what goes into a .h, the #ifndef guard and static.
Until now every program lived in one .c file. As a file grows, it gets hard to find anything in it. And when a second program needs the same functions, a copy means two places to fix the same bug. That's why a program is split into modules.
A module is two files:
daytime.h (a header file) – what the module can do: the list of functions, the constants and their descriptions. It is the contract for whoever uses the module.daytime.c – how: the function bodies.int to_minutes(int hour, int min); /* declaration */
int to_minutes(int hour, int min) { /* definition */
assert(is_time(hour, min));
return hour * 60 + min;
}
A declaration ("deklaracija") gives the name, the parameters and the return type, with a semicolon instead of a body. A definition ("apibrėžimas") has the body. You may declare a function several times, but define it exactly once in the whole program. Until now you wrote functions above main, so the definition was also the declaration.
#include: it simply pastes in the text of the named file..c file separately into an object file (.o; .obj in CLion on Windows). While translating main.c it sees only the declarations from daytime.h, and they are enough to check that a function is called correctly.So errors come in two kinds: the compiler's (in one file) and the linker's (across the whole program). In step 5 you'll cause both.
#define) that users need;Not: function bodies and variables. A .h is pasted into several .c files, so a body in it would turn into several definitions.
Every .h is wrapped in an include guard:
#ifndef DAYTIME_H
#define DAYTIME_H
/* ... deklaracijos ... */
#endif
The first time, DAYTIME_H isn't defined yet, so the contents are pasted in and DAYTIME_H gets defined. If the same file is included a second time (e.g. through another .h), the whole contents are skipped. You already saw such a guard in hw/05/numbers.h.
#include "daytime.h" – a project file: searched next to the .c file and in the project's folders.#include <stdio.h> – the standard library.A module's .c file includes its own .h first. Then the compiler checks that the declarations match the definitions: if the .h says int and the .c says double, you get conflicting types.
A function needed only inside the module is written with static:
static int nearly_equal(double a, double b) {
return a - b < 0.000001 && b - a < 0.000001;
}
It isn't in the .h file and other files can't see it, so another module may have its own nearly_equal. Without static, two functions with the same name in two files are a linker error: multiple definition.
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.