Translation Units
A translation unit is one source file after the preprocessor has finished with it, including every header it pulled in. The compiler translates one translation unit at a time and knows nothing about the others.
Detailed Explanation
The C standard does not have a concept of a "project". It has translation units. A translation unit, usually shortened to TU, is a single .c file with all its #include directives replaced by the text of those headers, all its macros expanded, and all its #if branches resolved. That expanded text is the only thing the compiler sees.
This is worth sitting with, because it explains most of C's build behavior. Your program might be thirty .c files, but the compiler never sees thirty files at once. It sees one TU, produces one object file, and forgets everything. Then it starts again on the next one with no memory of the first.
Two consequences follow immediately.
First, every name you use must be declared inside this TU. If main.c calls rectangle_area, the compiler in that TU needs a declaration telling it the parameter types and return type. It cannot go looking in area.c. In C89 a missing declaration was tolerated and the compiler guessed int for everything, which is why old code sometimes breaks in surprising ways. C99 removed implicit function declarations, so a missing declaration is now a diagnosable error.
Second, and more dangerous, type checking stops at the TU boundary. Suppose main.c declares int compute(void); while compute.c defines double compute(void). Each TU compiles cleanly, because each one is self-consistent. The linker matches only the name compute, since C does not encode parameter or return types into symbol names. You get a program that reads a floating-point return value as an integer. The standard calls this undefined behavior, and no tool in the default build will tell you.
C's answer to this is a rule about definitions across the whole program. An identifier with external linkage may have at most one external definition in the entire program, and all declarations of it must be compatible. C++ programmers know this family of rules as the One Definition Rule; C states it in less quotable language but means the same thing. Headers exist precisely so that the definer and every user read the same declaration from the same file, which makes violating the rule hard by accident.
The escape hatch is static at file scope, which gives an identifier internal linkage. A static function or variable is private to its TU. Two different .c files can each have static int call_count; and they are two unrelated objects. That is how C does encapsulation, and it is why static on a helper function is good hygiene rather than a micro-optimization.
The cost of this model is compile time and honesty about size. Compilation time scales with the size of the translation unit, not the size of the file you wrote. A 40-line .c file that includes several large headers can easily become a 60,000-line TU. Preprocess it with cc -E and count lines if you want to see where your build time goes.
Code Examples
/* counter.c — one translation unit.
* Check what the compiler actually sees:
* cc -E counter.c | wc -l
*/
#include <stdio.h>
static int call_count = 0; /* internal linkage: private to this TU.
Another .c file may have its own call_count. */
static void bump(void) /* also private; not visible to the linker */
{
call_count++;
}
int calls_so_far(void) /* external linkage: other TUs may declare and call it */
{
bump();
return call_count;
}
int main(void)
{
printf("%d\n", calls_so_far()); /* prints: 1 */
return 0;
}/* A mismatch the compiler cannot catch, spread over two TUs. */
/* ---- provider.c ---- */
double average(const int *values, int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++) sum += values[i];
return n > 0 ? sum / n : 0.0;
}
/* ---- consumer.c ---- */
/* Wrong declaration, written by hand instead of included from a header.
Both files compile. Linking succeeds. The behavior is undefined. */
int average(const int *values, int n);
int report(void)
{
int data[3] = {1, 2, 6};
return average(data, 3); /* reads a double return value as an int */
}
/* The fix: put one declaration in average.h and #include it in both files,
so the compiler checks the definition against the declaration it will
also hand to callers. */Common Gotchas
Hand-written declarations that disagree with the definition are undefined behavior
the linker matches names only, so nothing in a normal build catches it. Always declare a shared function in a header and include that header in the file that defines it, which forces the compiler to compare the two.
`static` at file scope means TU-private, not "one shared copy"
learners who know `static` from Java or C++ class members often read it backwards. On a *local* variable inside a function it means something different again: one instance that survives across calls.
One `.c` file that includes other `.c` files is still a single TU
this is the "unity build" trick. It speeds up whole-program builds and enables cross-file inlining without link-time optimization, at the price of losing incremental rebuilds and of `static` names colliding.
Redundant includes cost real build time
the TU is the compilation unit, so a header included by a header included by a header all lands in it. Include what you actually use and forward-declare with an incomplete `struct` type where you only need a pointer.