The preprocessor and headers.
Read includes, macros, conditional compilation, and header boundaries.
The idea behind the syntax.
The preprocessor transforms source before the compiler parses ordinary C declarations and expressions. #include brings header text into a translation unit, #define names a macro replacement, and conditional directives choose which text is included. This is powerful because it can configure an interface across platforms, but it works at the token level rather than with the type and scope rules of a normal function.
Headers should describe an interface that several source files can agree on: type declarations, function prototypes, constants, and carefully designed macros. A header guard prevents accidental repeated inclusion in one translation unit. The source file then supplies function definitions and, where appropriate, one definition of each external object. This division helps the compiler catch mismatched calls across files.
What C guarantees.
A function-like macro expands its arguments as source tokens. If an argument has a side effect and the macro uses it twice, the side effect may happen twice. Parenthesizing parameters and the entire replacement expression prevents many precedence surprises, but it does not give a macro ordinary function semantics. Prefer a typed function when it expresses the operation cleanly.
Conditional compilation is useful for optional platform code and feature selection, yet too many branches can make it hard to know which program is actually compiled. Keep a small, documented set of configuration choices and test the configurations that matter. The example uses a simple object-like macro for a fixed capacity, then performs an ordinary typed calculation in C code.
Why the detail matters.
Preprocessing happens before the compiler understands the types in an ordinary expression. A macro that looks like SQUARE(x) may expand x twice, so SQUARE(i++) can increment i twice or create a sequencing problem depending on the replacement. A static inline function can often provide a type-checked, single-evaluation alternative. A macro remains useful for conditional compilation, token-level generation, or a constant-like name where those abilities are needed.
A good header should be safe to include from several translation units. Guards prevent repeated declarations within one unit, while external function prototypes make caller and implementation agree on parameter types. Do not place a normal external variable definition in a shared header merely to make the name visible; that can create multiple definitions at link time. Put an extern declaration in the header and exactly one definition in a source file when shared storage is truly required.
See the rule in context.
#include <stdio.h>
#define LABEL_CAPACITY 24
int main(void) {
char label[LABEL_CAPACITY];
int n = snprintf(label, sizeof label, "Batch %d", 4);
if (n < 0 || (size_t)n >= sizeof label) return 1;
puts(label);
return 0;
}The preprocessor replaces LABEL_CAPACITY with 24 before compilation. The compiler then sees an array of 24 char values. snprintf writes Batch 4 and its terminator, returning 7. The condition verifies that the result fit, and puts prints the label with a newline. The macro names one compile-time constant; it does not implement the formatting behavior.
Where the rule stops.
Macros do not have parameter types, normal single-evaluation guarantees, or debugger-friendly call frames. Their apparent resemblance to functions can hide a different evaluation contract.
Keep this model.
Header design is part of modular program design. The next chapter follows one interface across translation units and explains why declarations, definitions, and linkage must agree rather than merely share a convenient name.