K&R C tutorials
K&R C is the C programming language as described in the first edition of
The C Programming Language by Brian Kernighan and Dennis Ritchie,
published in 1978. It is the C of early UNIX, written a decade before the
ANSI standard: functions have old-style parameter declarations and no
prototypes, a missing type means int, and there is no
void, const or enum. These tutorials
teach that dialect and the classic programs of the book, written anew. Modern
C is covered in the C tutorials.
The compilers
The examples are compiled with GCC in its oldest language
mode, with all common warnings turned on:
gcc -std=c89 -pedantic -Wall -Wextra. The sections about old
forms that GCC warns about, such as implicit int and implicit
function declarations, show the warnings.
The second compiler is the one the book was written for: Dennis Ritchie's
cc on V7 UNIX (1979), running on a PDP-11 simulated by SIMH.
Where the PDP-11, with its 16-bit int, double-precision
arithmetic, and arguments on the stack, behaves differently from GCC, a
section adds a second output block captured from the simulator.
Getting started
The introduction describes the history of the
language and its two compilers, and compiles the first programs.
Types and declarations covers the basic types,
symbolic constants, conversions, and implicit int, and
operators and expressions what is particular to
K&R C expressions, such as integer truth values, arithmetic in double
precision, and the old =+ operators.
Control flow shows the statements of the
language with integer conditions. Functions
defines functions with old-style parameter declarations and shows the calls
that a compiler without prototypes cannot check, and
scope and storage classes covers automatic,
external, static, and register variables.
Language features
K&R C is a small language. It has characters, integers of several sizes, and floating-point numbers; arrays, pointers, structures, and unions; and a preprocessor for constants, macros, and file inclusion. Input and output come from a library. The compiler trusts the programmer: arguments are not checked against parameters, conversions happen silently, and much behaviour is left to the machine. Every example in these tutorials is compiled with GCC 13.2 and run before its output is shown; the PDP-11 outputs are captured from V7 UNIX in the simulator.
last modified December 4, 2026