K&R C program structure
last modified December 5, 2026 K&R C 1978
A C program does not have to be in one file. The functions and external
variables can be spread over several source files, which are compiled
separately and then joined by the linker. This article splits programs into
files, shares variables between them with extern, hides names
with static, collects the declarations in a header file, and
rebuilds the program with make. The rules for
extern and static within a single file are
covered in the scope and storage classes
article.
A program in two files
The smallest program in several files has a main in one file
and a function it calls in another. The file with main declares
the function, as it would declare any function defined later in the same
file.
#include <stdio.h>
int sum();
int main()
{
static int v[] = { 4, 8, 15, 16, 23, 42 };
printf("sum: %d\n", sum(v, 6));
return 0;
}
int sum(); tells the compiler that sum is a
function returning int. Nothing in the declaration says where
the function is; the compiler leaves the address of the call open.
/* add up the n elements of v */
int sum(v, n)
int v[];
int n;
{
int i, s;
s = 0;
for (i = 0; i < n; i++)
s += v[i];
return s;
}
sum.c holds only the function. Both files are named on the
command line, and the compiler compiles each and links the results into one
program.
$ gcc -std=c89 -pedantic -Wall -Wextra -o total main.c sum.c $ ./total sum: 108
Separate compilation
The -c option stops after compiling: each source file becomes
an object file ending in .o, which contains the machine code
and a list of the names it defines and the names it still needs. A second
command passes the object files to the linker, which fills in the missing
addresses.
$ gcc -std=c89 -pedantic -Wall -Wextra -c sum.c $ gcc -std=c89 -pedantic -Wall -Wextra -c main.c $ ls *.o main.o sum.o $ gcc -o total main.o sum.o $ ./total sum: 108
External variables
External variables are shared between files in the same way as functions.
One file defines the variable: the definition sets aside the
storage and may initialize it. The other files declare it with
extern, which states its type but creates nothing. The next
module keeps running statistics of a series of numbers.
/* running statistics of a series of numbers */
int count = 0;
long total = 0;
int largest = 0;
int addval(x)
int x;
{
if (count == 0 || x > largest)
largest = x;
count++;
total += x;
return 0;
}
stats.c defines three external variables and the function
addval, which updates them. The definitions are the only
places where the variables are initialized.
#include <stdio.h>
extern int count, largest;
extern long total;
int addval();
int main()
{
static int data[] = { 12, 7, 31, 5, 18 };
int i;
for (i = 0; i < 5; i++)
addval(data[i]);
printf("count %d, total %ld, largest %d\n", count, total, largest);
return 0;
}
main.c declares the variables with extern and
reads them directly. If stats.c defined total as
an int, the declaration here would be wrong, and neither the
compiler nor the linker would notice: they see one file at a time, and the
object files carry only names, not types.
$ gcc -std=c89 -pedantic -Wall -Wextra -o stats main.c stats.c $ ./stats count 5, total 73, largest 31
Hiding names with static
External variables are visible to every file of the program, so any file
can change them. Declared static, an external variable or a
function is known only in its own file. A module can then keep its data to
itself and offer functions to reach it.
/* running statistics; the data is private to this file */
static int count = 0;
static long total = 0;
static int largest = 0;
static int bigger(a, b)
int a, b;
{
return a > b ? a : b;
}
int addval(x)
int x;
{
largest = count == 0 ? x : bigger(largest, x);
count++;
total += x;
return 0;
}
int getcount()
{
return count;
}
long gettotal()
{
return total;
}
int getlargest()
{
return largest;
}
The three variables and the helper function bigger are
static; only addval and the three functions that
return the results are visible outside the file. Another file may use the
names count or bigger for its own purposes
without a clash.
#include <stdio.h>
int addval(), getcount(), getlargest();
long gettotal();
int main()
{
static int data[] = { 12, 7, 31, 5, 18 };
int i;
for (i = 0; i < 5; i++)
addval(data[i]);
printf("count %d, total %ld, largest %d\n", getcount(), gettotal(),
getlargest());
return 0;
}
main.c declares the four functions it calls. One declaration
can name several functions of the same return type; gettotal
returns a long and needs a declaration of its own.
#include <stdio.h>
extern long total;
int main()
{
printf("total %ld\n", total);
return 0;
}
peek.c tries to read total directly. It compiles,
because the extern declaration is all the compiler sees, but
the linker finds no external total to connect it to.
$ gcc -std=c89 -pedantic -Wall -Wextra -c stats.c $ gcc -std=c89 -pedantic -Wall -Wextra -c main.c $ gcc -o stats main.o stats.o $ ./stats count 5, total 73, largest 31 $ gcc -std=c89 -pedantic -Wall -Wextra -c peek.c $ gcc -o peek peek.o stats.o /usr/bin/ld: peek.o: in function `main': peek.c:(.text+0x7): undefined reference to `total' collect2: error: ld returned 1 exit status
Mismatched declarations
Each file declares the functions it calls, and nothing checks that the
declarations agree with the definitions in other files. A third file adds
the mean of the values; it uses the public functions of
stats.c, which is unchanged.
/* the average of the values added so far */
int getcount();
long gettotal();
double mean()
{
int n;
n = getcount();
return n == 0 ? 0.0 : (double) gettotal() / n;
}
mean returns a double. The cast converts the
total before the division, so that the division is done in floating
point.
#include <stdio.h>
int addval(), getcount(), getlargest();
long gettotal();
int mean(); /* wrong: mean returns double */
int main()
{
static int data[] = { 12, 7, 31, 5, 18 };
int i;
for (i = 0; i < 5; i++)
addval(data[i]);
printf("count %d, total %ld, largest %d\n", getcount(), gettotal(),
getlargest());
printf("mean %d\n", mean());
return 0;
}
The declaration of mean in main.c says it returns
an int. The program compiles without a warning and prints
nonsense: main takes an int from the place where
integers are returned, while mean left its
double elsewhere.
$ gcc -std=c89 -pedantic -Wall -Wextra -o stats main.c stats.c mean.c $ ./stats count 5, total 73, largest 31 mean 858993459
Header files
The cure is to write each declaration once, in a header file,
and include it with #include in every file that needs it. A
header named in double quotes is looked for in the directory of the source
file first.
/* stats.h: running statistics of a series of numbers */ int addval(); int getcount(); long gettotal(); int getlargest(); double mean();
stats.h declares the functions of the module. The data stays
static in stats.c, so the header does not mention
it.
#include <stdio.h>
#include "stats.h"
int main()
{
static int data[] = { 12, 7, 31, 5, 18 };
int i;
for (i = 0; i < 5; i++)
addval(data[i]);
printf("count %d, total %ld, largest %d\n", getcount(), gettotal(),
getlargest());
printf("mean %f\n", mean());
return 0;
}
main.c includes the header instead of declaring the functions
itself, and prints the mean with %f. stats.c and
mean.c begin with #include "stats.h" too. The
compiler then sees the declaration and the definition of each function
together and reports a difference: had stats.h declared
int mean();, compiling mean.c would stop with
conflicting types for 'mean'. The header covers only return
types. A call such as addval(data[i], 2) still compiles
silently, as unchecked calls do in K&R C; the
debugging article checks them with
lint.
$ gcc -std=c89 -pedantic -Wall -Wextra -o stats main.c stats.c mean.c $ ./stats count 5, total 73, largest 31 mean 14.600000
Building with make
When a program has many files, recompiling all of them after every change
is slow, and remembering which files depend on a changed header is error
prone. The make program, written at Bell Labs in 1976, reads
the dependencies from a file called Makefile and rebuilds only
what is out of date.
CC = gcc CFLAGS = -std=c89 -pedantic -Wall -Wextra stats: main.o stats.o mean.o $(CC) -o stats main.o stats.o mean.o main.o: main.c stats.h stats.o: stats.c stats.h mean.o: mean.c stats.h clean: rm -f stats main.o stats.o mean.o
A rule names a target, a colon, and the files it depends on; the command
lines below it start with a tab. stats depends on the three
object files and is linked from them. The object files have no commands:
make knows how to compile a .c file into a
.o file and uses the variables CC and
CFLAGS for it. Each object file also depends on
stats.h. clean is a target with no file, used to
remove the results.
$ make gcc -std=c89 -pedantic -Wall -Wextra -c -o main.o main.c gcc -std=c89 -pedantic -Wall -Wextra -c -o stats.o stats.c gcc -std=c89 -pedantic -Wall -Wextra -c -o mean.o mean.c gcc -o stats main.o stats.o mean.o $ ./stats count 5, total 73, largest 31 mean 14.600000 $ touch mean.c $ make gcc -std=c89 -pedantic -Wall -Wextra -c -o mean.o mean.c gcc -o stats main.o stats.o mean.o $ touch stats.h $ make gcc -std=c89 -pedantic -Wall -Wextra -c -o main.o main.c gcc -std=c89 -pedantic -Wall -Wextra -c -o stats.o stats.c gcc -std=c89 -pedantic -Wall -Wextra -c -o mean.o mean.c gcc -o stats main.o stats.o mean.o $ make make: 'stats' is up to date.
Duplicate definitions
A common mistake is to write the same definition in two files, for example
int count; where one of them should say
extern int count;.
int count;
int bump()
{
count++;
return 0;
}
count.c defines count and a function that
increments it.
#include <stdio.h>
int count; /* should be: extern int count; */
int bump();
int main()
{
count = 5;
bump();
printf("count %d\n", count);
return 0;
}
main.c also defines count instead of declaring
it. The book requires exactly one definition among all the files. GCC
compiles both files, and the linker refuses to put the two definitions
together.
$ gcc -std=c89 -pedantic -Wall -Wextra -c count.c $ gcc -std=c89 -pedantic -Wall -Wextra -c main.c $ gcc -o counter main.o count.o /usr/bin/ld: count.o:(.bss+0x0): multiple definition of `count'; main.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status
The UNIX linker of the PDP-11 merged definitions without an initializer
into one variable, as FORTRAN merged its COMMON blocks. The
program links, and both files use the same count. Many old
programs relied on this; GCC accepts it with the -fcommon
option.
$ cc -c count.c $ cc -c main.c $ cc main.o count.o $ a.out count 6
Source
Dennis Ritchie: C Reference Manual (1975)
In this article we have built K&R C programs from several files:
compiling and linking, separate compilation into object files, external
variables shared with extern, names hidden with
static, mismatched declarations, header files,
make, and duplicate definitions. The include guards that
protect a header from being read twice belong to
the preprocessor, the next article.
Author
List all K&R C tutorials.