The project's parser, add_session and total are needed by two programs: the report, and a test program with its own main. Copying the functions into both would mean two copies to keep in step. Instead, one .c file holds the helper definitions, each program's main lives in its own .c file, and a header file, ending in .h, carries the declarations that let them call each other.
Predict and run
#include <stdio.h>
int twice(int n);
int twice(int n) {
return n * 2;
}
int main(void) {
printf("%d\n", twice(6));
return 0;
}Check your prediction
12The first twice line, ending in a semicolon, is a declaration: the function's name, parameters and return type. The second, with a body, is the definition. Splitting a program puts the declaration in a header that every file can include, and the definition in exactly one .c file.
How the files connect
Building a multi-file program has two stages.
- Compiling. Each
.cfile is compiled on its own into an object file.#include "report.h"pastes the header's text in first, so each call inmain.cis checked against a declaration. The body is not needed yet. - Linking. The linker joins the object files and connects each call to the one definition with that name. If no file defines it, compiling succeeds and linking fails:
undefined reference to `total_minutes'
collect2: error: ld returned 1 exit statusThe wording differs between systems, but the meaning is "declared, called, never defined". Two definitions with the same name are also a link error, which is why the test executable is linked with report.c but not with main.c: each has a main.
The header in the editor below starts with a header guard: #ifndef REPORT_H, #define REPORT_H, and #endif at the end. On the first inclusion REPORT_H is undefined, so the contents are used and the name gets defined; any later inclusion in the same file skips everything, so struct Session is not defined twice. Include your own headers with quotes and standard headers with angle brackets. Never #include a .c file, or its definitions end up in several object files.
Run the shared helper tests
These tabs hold the same report.h and report.c that the final project downloads, with the test program test_report.c as the entry file.
Check your prediction
Report checks passedOpen each tab before you go on. report.h holds the guard, struct Session and one commented prototype per helper; report.c includes it and defines each helper once; test_report.c includes it too and has its own main.
What you will use
- Declarations as contracts. Each prototype in
report.hhas a comment stating its precondition or its failure behavior. Read them as promises. - Definitions in one place.
report.cimplements every declared function and includes its own header, so the compiler checks that the two agree. - Two entry files.
main.candtest_report.ceach have amain, and each is linked withreport.c, never with each other.
Plan before you type
- Read
report.habove and write down what each function promises on failure. - Run the helper tests and confirm the success line.
- For the practice task, write
minutes.hfirst: the guard, then one commented declaration per function. - Write the assertions in
main.c, run, and read the link error: it lists exactly the functionsminutes.cstill has to define. - Write the definitions in
minutes.cuntil the assertions pass.
Prove it can fail
In the multi-file editor above, rename total_minutes to total_minute in report.c only and run. Compiling still succeeds for test_report.c, but linking fails with an undefined reference, because the header promised a function that no file defines. Restore it. Then change MAX_MINUTES in report.h to 179: the assertion that accepts "180\n" fails, which shows the header's value reaches both files. Reset when you are done.
Mistakes worth checking for
call to undeclared function. The file calls a function without including the header that declares it, or the declaration comes after the call.undefined referenceorUndefined symbols. The declaration exists but no linked file defines it: a misspelled name, orreport.cleft out of the build command.duplicate symbolormultiple definition of main. Two entry files were linked together, or a.cfile was included in another.redefinition of 'struct Session'. A header without a guard was included twice in one file.
Practice
Your turn
Split two helpers across three files, the way report.h, report.c and test_report.c are split. In the tabs below:
minutes.h: a header guard (MINUTES_H) and the declarationsint valid_minutes(int minutes);andint accepted_total(const int values[], int count);, each with a one-line comment stating its contract.minutes.c: includes"minutes.h"and defines both functions.valid_minutesaccepts 1 through 180;accepted_totalsums only the valid durations, usingvalid_minutes.main.c: for{25, 0, 181, 10}, assert the total is 35, that count 0 gives 0 and count 1 gives 25, then print:
Total: 35
Accepted total checks passedHint 1: the idea
The header is the only file the other two share. main.c needs just the declarations to compile its calls, and the linker then finds the one definition of each function in minutes.c. The guard is three lines around everything else in the header.
Hint 2: a first step
Fill in minutes.h only, then write one assertion in main.c and run. Compiling now succeeds, and linking fails with an undefined reference (or Undefined symbols) naming accepted_total: the header promised a function that no file defines yet. That message is your to-do list for minutes.c.
Show one solution
Total: 35
Accepted total checks passedmain.c compiles because minutes.h declares both functions, and the program links because minutes.c defines each exactly once. minutes.c includes its own header, so if a definition ever drifted from its declaration (say long accepted_total(...)), the compiler would report the conflict instead of the program silently misbehaving. These prototypes could be repeated harmlessly even without the guard, but a header that defines a type, as report.h defines struct Session, cannot, so every header gets one. The values 0 and 181 sit inside the data, so a range one too wide would change the total. Tomorrow you assemble the whole report from these files.
Completion is saved in this browser only. It does not measure mastery.