ZetCode

Introduction to K&R C

last modified December 5, 2026 K&R C 1978

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 language of early UNIX, written before C had a standard. This article introduces the language, the two compilers used in the series, and the first programs.

What is K&R C

Dennis Ritchie developed C at Bell Labs between 1969 and 1973. It grew out of B, a typeless language by Ken Thompson, which in turn came from BCPL. Ritchie added types such as char, int and float, structures, and a compiler that produced fast code for the PDP-11. In 1973 the UNIX kernel was rewritten in C, and the language spread with the operating system to universities and companies.

For years the only description of the language was the compiler and a short reference manual by Ritchie. In 1978 Kernighan and Ritchie published The C Programming Language. The book was small, clear, and full of working programs, and it served as the de facto definition of C for a decade. Programmers called the book, and the language it describes, K&R.

The American National Standards Institute standardised C in 1989, and the second edition of the book (1988) describes that standard language, ANSI C. This series covers the language of the first edition. ANSI and modern C are taught in the C tutorials, and the porting K&R C to modern C article shows how to bring a K&R program up to date.

The language of the book

K&R C is a small language that trusts the programmer. Function parameters are named in the parameter list and declared between the list and the body. A missing type means int, so a function without a return type returns an int. Named constants are #define lines, comments are written between /* and */, and a library function that returns anything other than an int is declared by hand before it is called. The compiler checks little: it does not know the parameters of a function, so it checks neither the number nor the types of the arguments of a call.

The compilers

The examples in this series are compiled with GCC 13.2, with all common warnings turned on:

gcc -std=c89 -pedantic -Wall -Wextra -o program program.c

GCC has no mode for the language of 1978. The -std=c89 option selects the oldest language it knows, which still accepts old-style function definitions and implicit int. The -pedantic option rejects GCC extensions, and -Wall -Wextra turn on the warnings. The programs use only what the 1978 book describes; GCC would accept more.

The second compiler is the one the book was written for: Dennis Ritchie's cc on the Seventh Edition of UNIX (V7, 1979), running on a PDP-11 computer simulated by SIMH. On the PDP-11 an int has 16 bits, floating-point arithmetic is always done in double precision, and function arguments travel on the stack. Where this makes a program behave differently, a section shows a second output block, labelled PDP-11, captured from the simulator. The V7 compiler writes its program to a.out.

Three conventions keep the programs free of GCC warnings. Every program defines int main() and ends it with return 0;, where the book wrote main() without a return. Functions that return nothing useful are declared int and return 0, because there is no void. And the programs include headers such as math.h and string.h instead of declaring library functions by hand. The types and functions articles show what GCC says about the original forms.

The first program

A C program is a set of functions. Execution starts in the function called main. The first program prints one line of text.

first.c
#include <stdio.h>

int main()
{
    printf("Hello there!\n");
    return 0;
}

The #include <stdio.h> line makes the standard input and output library available. int main() begins the definition of main; the empty parentheses mean that it takes no arguments, and the body is enclosed in braces. printf prints its argument, and the \n sequence ends the line. The statement return 0; ends the program and reports success to the operating system. Every statement ends with a semicolon.

$ gcc -std=c89 -pedantic -Wall -Wextra -o first first.c
$ ./first
Hello there!

Compiling and running

C is a compiled language. The gcc command translates the source file into an executable file, and the shell runs it. The -o option names the executable; without it, GCC writes a.out, the traditional name from early UNIX. printf does not end the line by itself, so one line can be built by several calls.

history.c
#include <stdio.h>

int main()
{
    printf("C was designed by Dennis Ritchie ");
    printf("at Bell Labs.\n");
    printf("The book by Kernighan and Ritchie ");
    printf("appeared in 1978.\n");
    return 0;
}

The first and third calls print text without \n, so the output of the next call continues on the same line. The program prints two lines from four calls. The string inside the quotes is a string literal; it cannot span lines in the source code.

$ gcc -std=c89 -pedantic -Wall -Wextra -o history history.c
$ ./history
C was designed by Dennis Ritchie at Bell Labs.
The book by Kernighan and Ritchie appeared in 1978.

On V7 UNIX the same source is compiled with cc, and the program is run as a.out. The output is the same.

$ cc history.c
$ a.out
C was designed by Dennis Ritchie at Bell Labs.
The book by Kernighan and Ritchie appeared in 1978.

Comments

A comment begins with /* and ends with */. The compiler ignores everything in between. A comment may span several lines, follow a statement, or turn a line of code off.

comments.c
#include <stdio.h>

/*
 * comments.c - prints the name of the language.
 * A comment may span several lines.
 */

int main()
{
    printf("K&R C\n");    /* a comment after a statement */
    /* printf("This line is commented out.\n"); */
    return 0;
}

The block at the top describes the file. The leading asterisks inside it are only decoration. The second printf is inside a comment and is not compiled. Comments do not nest: the first */ ends the comment, so code that already contains comments cannot be commented out this way.

$ gcc -std=c89 -pedantic -Wall -Wextra -o comments comments.c
$ ./comments
K&R C

No line comments

K&R C has only the /* */ comment. A comment that starts with // and runs to the end of the line is not part of the language. GCC accepts it as an extension, but with -std=c89 -pedantic it is an error.

line_comment.c
#include <stdio.h>

int main()
{
    printf("K&R C\n");    // a line comment
    return 0;
}

The program is the same as before except for the comment. The compiler reports the comment and produces no executable.

$ gcc -std=c89 -pedantic -Wall -Wextra -o line_comment line_comment.c
line_comment.c: In function ‘main’:
line_comment.c:5:27: error: C++ style comments are not allowed in ISO C90
    5 |     printf("K&R C\n");    // a line comment
      |                           ^
line_comment.c:5:27: note: (this will be reported only once per input file)

Printing numbers

The first argument of printf is a format string. A conversion specification, a % followed by a letter, marks a place where the next argument is printed. %d prints an integer in decimal, %c a single character, and %s a string.

numbers.c
#include <stdio.h>

int main()
{
    printf("C appeared in %d.\n", 1972);
    printf("The book appeared %d years later.\n", 1978 - 1972);
    printf("%d + %d = %d\n", 2, 3, 2 + 3);
    printf("%d * %d = %d\n", 12, 12, 12 * 12);
    printf("The first letter is %c.\n", 'K');
    printf("The language is %s.\n", "C");
    return 0;
}

The arguments are matched with the conversions from left to right. An argument may be an expression such as 1978 - 1972, which is computed before it is printed. A character constant is written in single quotes, 'K', and a string in double quotes. GCC checks that each argument fits its conversion; the PDP-11 compiler does not. The formatted output article covers the conversions in detail.

$ gcc -std=c89 -pedantic -Wall -Wextra -o numbers numbers.c
$ ./numbers
C appeared in 1972.
The book appeared 6 years later.
2 + 3 = 5
12 * 12 = 144
The first letter is K.
The language is C.

Escape sequences

Some characters cannot be typed into a string directly. An escape sequence, a backslash followed by a character, stands for them: \n is a newline, \t a tab, \" a double quote, and \\ a backslash.

escapes.c
#include <stdio.h>

int main()
{
    printf("Edition\tYear\n");
    printf("first\t1978\n");
    printf("second\t1988\n");
    printf("The book is called \"The C Programming Language\".\n");
    printf("A backslash: \\\n");
    return 0;
}

The tabs align the two columns of the small table. The quotes around the book title would end the string, so they are escaped. A single backslash starts an escape sequence, so a backslash in the output is written as two.

$ gcc -std=c89 -pedantic -Wall -Wextra -o escapes escapes.c
$ ./escapes
Edition	Year
first	1978
second	1988
The book is called "The C Programming Language".
A backslash: \

The exit status

The value that main returns becomes the exit status of the program. By UNIX convention, 0 means success and any other value an error. The shell keeps the status of the last command in the $? variable.

exit_status.c
#include <stdio.h>

int main()
{
    printf("Checking the input files.\n");
    printf("Two files are missing.\n");
    return 2;
}

The program reports a failure with the status 2. Scripts and tools such as make test this value to decide whether to continue. The book's programs wrote main() without a return; the types and declarations article shows that the exit status of such a program is undefined.

$ gcc -std=c89 -pedantic -Wall -Wextra -o exit_status exit_status.c
$ ./exit_status
Checking the input files.
Two files are missing.
$ echo $?
2

Source

Dennis Ritchie: The Development of the C Language

GCC: Options Controlling C Dialect

The C Programming Language on Wikipedia

SIMH: software kits, including UNIX V7 for the PDP-11

In this article we have introduced K&R C, the language of the 1978 book, and its two compilers, GCC and cc on V7 UNIX, and compiled the first programs. The next article, K&R C types and declarations, covers the basic types, variables, and symbolic constants. The introduction to C starts the modern C series.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all K&R C tutorials.