Step 1 of 6
Headers and include guards
Real C programs are split into many files. Each module has two parts:
- a header (
score.h) with declarations: types, function prototypes, constants. It's the module's public interface. - a source file (
score.c) with the definitions: the actual function bodies.
Other files #include "score.h" to use the module. The compiler builds each .c file separately, then the linker joins them.
/* score.h */
#ifndef SCORE_H
#define SCORE_H
typedef struct { int kills, deaths; } Score;
double kd_ratio(Score s);
#endif
That #ifndef / #define / #endif wrapper is an include guard. Headers often get included twice (a.h includes b.h, and so does main.c), and defining the struct twice is an error. The guard makes the second copy vanish.
The editor here holds a single file, so this program pastes the "header" in twice to simulate a double include. Your turn: complete the guard so it compiles.