So far each program has been one file. The project has three: analyzer.h, analyzer.c and main.c, plus test_analyzer.c. The reason is reuse with control. The tests and the application both need the parser and the collection, so those live in a library: analyzer.h declares what callers may use, and analyzer.c defines how it works. The application, main.c, decides what to read, what to print and when to stop. Drawing that line well means a test can call analyzer_append directly without arranging stdin, and a change to the report's wording cannot break the collection.
Predict and run
Read the program before pressing Run. Write down its output and decide which function would be declared in a header.
#include <stdio.h>
static int valid(int value) { return value >= 1 && value <= 180; }
int accepted(int value) { return valid(value); }
int main(void) {
printf("%d\n", accepted(25));
return 0;
}Check your prediction
1accepted is the public function: other files would call it, so its declaration belongs in a header. valid is marked static, which hides it inside this file. Callers do not need it, and the implementation can rename or remove it without breaking anyone.
What a header contains
A header is text that #include pastes into each file that names it. It should hold what callers need to compile their calls, and nothing else:
- Type definitions, such as
Record,AnalyzerStatusandAnalyzer. - Function declarations (prototypes) with their contracts written as comments.
- Constants callers use, such as
TOPIC_CAPACITYandANALYZER_LIMIT.
Function definitions go in the .c file. If a header defined a non-static function and two .c files included it, the program would contain two definitions of the same name and the link step would fail. An include guard stops one file from seeing a header's contents twice, which would repeat the type definitions:
#ifndef ANALYZER_H
#define ANALYZER_H
/* types and declarations */
#endifInclude your own header
analyzer.c starts with #include "analyzer.h". That is not decoration: it makes the compiler compare each definition with its public declaration. If the header says long analyzer_total(const Analyzer *analyzer); but the definition returns int, the compiler reports conflicting types for 'analyzer_total'. Without the include, both files compile separately, the linker joins them by name only, and callers read a return value of the wrong type, which is undefined behavior with no diagnostic.
static means "private to this file"
A function at file scope normally has external linkage: its name is visible to the whole program. Two files that both define a helper named whitespace would collide:
duplicate symbol '_whitespace' in:
analyzer.o
main.o
ld: 1 duplicate symbolMarking the helper static gives it internal linkage: each file may have its own, and no other file can call it. In the project, whitespace, valid_record and compare_records are static; they are implementation details. Only functions declared in the header are part of the contract.
Policy belongs to the application
A library function that prints "Storage unavailable" and calls exit(1) would be useless in a test that wants to check what happens after a failure, and would force one wording on every program. So the library reports outcomes with AnalyzerStatus and changes nothing it did not promise to change. main.c decides that an invalid line is counted, that ANALYZER_NOMEM is fatal, and what message goes to stderr.
How strong is the boundary?
Analyzer is a public struct, so callers can see and even write its fields. That is deliberate for learning, since you can inspect length and capacity in tests. The invariant is still the caller's obligation: change an Analyzer only through analyzer_init, analyzer_append, analyzer_remove and analyzer_destroy. A stronger design is an opaque type: the header says only typedef struct Analyzer Analyzer;, and the fields are defined in analyzer.c. Callers then cannot touch fields or copy the struct, but they also cannot declare one as a local variable, so the library must allocate it.
Practice
Your turn
The workspace is one file split into a "header" section and an "implementation" section. Write the public functions DurationStatus duration_check(int minutes) and const char *duration_message(DurationStatus status), using a private static int in_range(int value, int low, int high) helper. The program must print 0: too short, 25: ok, 180: ok, 181: too long.
#include <stdio.h>
/* ---- duration.h: what callers may use ---- */
typedef enum { DURATION_OK, DURATION_TOO_SHORT, DURATION_TOO_LONG } DurationStatus;
DurationStatus duration_check(int minutes);
const char *duration_message(DurationStatus status);
/* ---- duration.c: how it works ---- */
/* Write in_range, duration_check and duration_message here. */
/* ---- main.c: the application ---- */
int main(void) {
int inputs[4] = {0, 25, 180, 181};
for (int i = 0; i < 4; ++i) {
printf("%d: %s\n", inputs[i], duration_message(duration_check(inputs[i])));
}
return 0;
}Hint 1: the idea
main uses only the two declared functions. If you could delete in_range and replace it with something else without touching main, the boundary is in the right place.
Hint 2: a first step
Write duration_message first, a switch over the three statuses, and test it from main by passing each status directly. Then write in_range, and make duration_check decide between its three answers using only in_range and one more comparison.
Show one solution
#include <stdio.h>
/* ---- duration.h: what callers may use ---- */
typedef enum { DURATION_OK, DURATION_TOO_SHORT, DURATION_TOO_LONG } DurationStatus;
DurationStatus duration_check(int minutes);
const char *duration_message(DurationStatus status);
/* ---- duration.c: how it works ---- */
static int in_range(int value, int low, int high) {
return value >= low && value <= high;
}
DurationStatus duration_check(int minutes) {
if (in_range(minutes, 1, 180)) { return DURATION_OK; }
return minutes < 1 ? DURATION_TOO_SHORT : DURATION_TOO_LONG;
}
const char *duration_message(DurationStatus status) {
switch (status) {
case DURATION_OK: return "ok";
case DURATION_TOO_SHORT: return "too short";
case DURATION_TOO_LONG: return "too long";
}
return "unknown";
}
/* ---- main.c: the application ---- */
int main(void) {
int inputs[4] = {0, 25, 180, 181};
for (int i = 0; i < 4; ++i) {
printf("%d: %s\n", inputs[i], duration_message(duration_check(inputs[i])));
}
return 0;
}0: too short
25: ok
180: ok
181: too longThe declarations at the top play the header's role: because they come before the definitions, the compiler checks that each definition matches. In a real split, in_range would stay static in duration.c and other files could not call it. The library returns a status; only main prints. Next, you will give a function with several resources one cleanup path that runs on every exit.
Completion is saved in this browser only. It does not measure mastery.