Skip to the task
Layout
Back to classic view
READTask 1 of 26 · step 1: A program in several filesA program in several files
A program in several files in practice/06/

Declaration and definition, how a program is built (preprocessor, compiler, linker), what goes into a .h, the #ifndef guard and static.

Details & hint

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.

Declaration and definition

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.

How a program of several files is built

main.cdaytime.hdaytime.c #include#include compilercompiler main.odaytime.o linker p07_duration.exe
  1. The preprocessor carries out #include: it simply pastes in the text of the named file.
  2. The compiler translates each .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.
  3. The linker joins the object files and libraries into one program and finds the definition for every call.

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.

What goes into a .h

  • function declarations;
  • constants (#define) that users need;
  • descriptions: the contracts, and from step 6 Doxygen.

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 "..." and #include <...>

  • #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.

static: for this file only

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.

practice 06 hall.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#define ROWS 4
#define SEATS 6
/* How many free seats (0) there are in a row.
Before: 0 <= row < ROWS.
After: a number from 0 to SEATS. */
int free_seats(int hall[][SEATS], int row) {
assert(row >= 0 && row < ROWS);
int count = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[row][s] == 0) {
count++;
}
}
return count;
}
/* The first row that has k free seats side by side.
Before: 1 <= k <= SEATS.
After: the row's index, or -1 if there is none. */
int find_row(int hall[][SEATS], int k) {
assert(k >= 1 && k <= SEATS);
for (int r = 0; r < ROWS; r++) {
int in_a_row = 0;
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 0) {
in_a_row++;
if (in_a_row == k) {
return r;
}
} else {
in_a_row = 0;
}
}
}
return -1;
}
int main(void) {
int hall[ROWS][SEATS] = {
{1, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0},
};
for (int r = 0; r < ROWS; r++) {
printf("Row %d: ", r + 1);
for (int s = 0; s < SEATS; s++) {
if (hall[r][s] == 1) {
printf("#");
} else {
printf(".");
}
}
printf(" free: %d\n", free_seats(hall, r));
}
for (int k = 2; k <= 5; k++) {
int r = find_row(hall, k);
if (r == -1) {
printf("No %d seats side by side.\n", k);
} else {
printf("%d seats side by side: row %d.\n", k, r + 1);
}
}
return 0;
}