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 main.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include "input.h"
#define DAYS 7
/* Reads n temperatures into the array t.
Before: 1 <= n <= DAYS.
After: t[0]..t[n - 1] are filled. */
void read_temperatures(int t[], int n) {
assert(n >= 1 && n <= DAYS);
for (int i = 0; i < n; i++) {
printf("%d. ", i + 1);
t[i] = read_int("Temperature: ");
}
}
/* Prints n values of the array on one line, separated by a space.
Before: 0 <= n <= DAYS. */
void print_array(const int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", t[i]);
}
printf("\n");
}
/* The arithmetic mean.
Before: 1 <= n <= DAYS.
After: (t[0] + ... + t[n - 1]) / n. */
double average(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += t[i];
}
return (double)sum / n;
}
/* The index of the warmest day (the first one, if several are equal).
Before: 1 <= n <= DAYS.
After: t[i] >= t[j] for every j. */
int warmest(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int best = 0;
for (int i = 1; i < n; i++) {
if (t[i] > t[best]) {
best = i;
}
}
return best;
}
/* Linear search: the index of the first such value.
Before: 0 <= n <= DAYS.
After: i such that t[i] == value, or -1 if there is none. */
int find(const int t[], int n, int value) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (t[i] == value) {
return i;
}
}
return -1;
}
/* Sorts in ascending order, by selection.
Before: 0 <= n <= DAYS.
After: t[0] <= t[1] <= ... <= t[n - 1]. */
void sort_array(int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (t[j] < t[min]) {
min = j;
}
}
int temp = t[i];
t[i] = t[min];
t[min] = temp;
}
}
int main(void) {
int temperatures[DAYS];
read_temperatures(temperatures, DAYS);
printf("Temperatures: ");
print_array(temperatures, DAYS);
printf("Average: %.2f\n", average(temperatures, DAYS));
int w = warmest(temperatures, DAYS);
printf("Warmest: day %d, %d C\n", w + 1, temperatures[w]);
int sorted[DAYS];
for (int i = 0; i < DAYS; i++) {
sorted[i] = temperatures[i];
}
sort_array(sorted, DAYS);
printf("Sorted: ");
print_array(sorted, DAYS);
printf("Median: %d C\n", sorted[DAYS / 2]);
int target = read_int("Which temperature to look for? ");
int pos = find(temperatures, DAYS, target);
if (pos == -1) {
printf("%d C was not on any day.\n", target);
} else {
printf("%d C was first on day %d.\n", target, pos + 1);
}
return 0;
}