ZetCode

Porting K&R C to modern C

last modified December 5, 2026 K&R C 1978

The language of the 1978 book did not stay as it was. The ANSI committee standardized C in 1989; the standard, C89, was adopted by ISO as C90 and described in the second edition of the book. It kept almost all of K&R C and added function prototypes, void, const, enum, and the standard headers. C99 added // comments, declarations anywhere in a block, long long, and stdint.h, and C23 finally removed the old-style function definitions. This article compares the two languages and shows how a K&R program is brought up to date. The modern C series covers the newer language in full in the C tutorial.

Old code under a newer standard

A K&R program written in the style of the book, with implicit int and undeclared functions, still compiles with GCC 13 under the C99 standard, but every old habit gets a warning.

oldstyle.c
#include <stdio.h>

power(base, n)          /* implicit int everywhere */
{
    int p;

    for (p = 1; n > 0; n--)
        p = p * base;
    return p;
}

main()
{
    printf("%d %d\n", power(2, 10), abs(-7));
    return 0;
}

power and main have no return type, the parameters of power have no declarations, and abs is called without a declaration. C99 removed implicit int and implicit function declarations, and GCC says so for each of them. It also suggests the header that declares abs. The program still runs. GCC 14 turns these warnings into errors in the C99 and later modes, so old code must be fixed or compiled with -std=c89.

$ gcc -std=c99 -o oldstyle oldstyle.c
oldstyle.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    3 | power(base, n)          /* implicit int everywhere */
      | ^~~~~
oldstyle.c: In function ‘power’:
oldstyle.c:3:1: warning: type of ‘base’ defaults to ‘int’ [-Wimplicit-int]
oldstyle.c:3:1: warning: type of ‘n’ defaults to ‘int’ [-Wimplicit-int]
oldstyle.c: At top level:
oldstyle.c:12:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
   12 | main()
      | ^~~~
oldstyle.c: In function ‘main’:
oldstyle.c:14:37: warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration]
   14 |     printf("%d %d\n", power(2, 10), abs(-7));
      |                                     ^~~
oldstyle.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘abs’
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 | 
$ ./oldstyle
1024 7

Function prototypes

The most important addition of ANSI C is the prototype, a function declaration that lists the types of the parameters. The definition names the parameters in the same list, instead of declaring them between the list and the body.

proto.c
#include <stdio.h>

double square(double x);        /* a prototype */

int main(void)
{
    printf("%g\n", square(3));  /* 3 is converted to 3.0 */
    printf("%g\n", square(2.5));
    return 0;
}

double square(double x)
{
    return x * x;
}

The prototype before main tells the compiler that square takes a double, so the call square(3) converts the int 3 to 3.0, as an assignment would. In K&R C the same call passed an int where a double was expected and returned garbage, as the functions article showed. A function without parameters is written int main(void); an empty list in a declaration means that the parameters are unknown.

$ gcc -std=c89 -pedantic -Wall -Wextra -o proto proto.c
$ ./proto
9
6.25

Prototypes check calls

With a prototype in scope, a call with the wrong number of arguments or an argument that cannot be converted is an error, not a run-time surprise.

badcall.c
#include <stdio.h>

double square(double x);

int main(void)
{
    printf("%g\n", square(3, 4));
    printf("%g\n", square("3"));
    return 0;
}

double square(double x)
{
    return x * x;
}

The first call has an extra argument, the second passes a string. In K&R C both compile; here neither does. Calls across files, which only lint could check (see the debugging article), are checked by the compiler once every file includes a header with the prototypes.

$ gcc -std=c89 -pedantic -Wall -Wextra -o badcall badcall.c
badcall.c: In function ‘main’:
badcall.c:7:20: error: too many arguments to function ‘square’
    7 |     printf("%g\n", square(3, 4));
      |                    ^~~~~~
badcall.c:3:8: note: declared here
    3 | double square(double x);
      |        ^~~~~~
badcall.c:8:27: error: incompatible type for argument 1 of ‘square’
    8 |     printf("%g\n", square("3"));
      |                           ^~~
      |                           |
      |                           char *
badcall.c:3:22: note: expected ‘double’ but argument is of type ‘char *’
    3 | double square(double x);
      |               ~~~~~~~^

Finding old-style code

In a large program, the old definitions must first be found. GCC has warnings for them, which are not part of -Wall or -Wextra.

findold.c
#include <stdio.h>

int add(a, b)
int a, b;
{
    return a + b;
}

int twice();

int main()
{
    printf("%d %d\n", add(2, 3), twice(4));
    return 0;
}

int twice(n)
int n;
{
    return 2 * n;
}

The file has two old-style definitions, a declaration without parameter types, and int main(). -Wold-style-definition reports the definitions, and -Wstrict-prototypes every declaration that is not a prototype. A third option, -Wmissing-prototypes, reports external functions that are defined without an earlier prototype; it finds functions that belong in a header.

$ gcc -std=c89 -pedantic -Wall -Wextra -Wold-style-definition -Wstrict-prototypes -Wmissing-prototypes -c findold.c
findold.c:3:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
    3 | int add(a, b)
      |     ^~~
findold.c: In function ‘add’:
findold.c:3:5: warning: old-style function definition [-Wold-style-definition]
findold.c: At top level:
findold.c:9:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
    9 | int twice();
      | ^~~
findold.c:11:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
   11 | int main()
      |     ^~~~
findold.c: In function ‘main’:
findold.c:11:5: warning: old-style function definition [-Wold-style-definition]
findold.c: At top level:
findold.c:17:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
   17 | int twice(n)
      |     ^~~~~
findold.c: In function ‘twice’:
findold.c:17:5: warning: old-style function definition [-Wold-style-definition]

Library headers

The 1978 programs declared the library functions they used by hand, because the headers stdlib.h and string.h did not exist yet. ANSI C put a prototype of every library function into a standard header. A hand declaration now conflicts with what the compiler knows about the function.

handdecl.c
#include <stdio.h>

char *malloc();
char *strcpy();

int main()
{
    char *p;

    p = malloc(10);
    strcpy(p, "tide");
    printf("%s\n", p);
    return 0;
}

GCC knows the types of malloc and strcpy as built-in functions. It reports that malloc returns void * and takes a size_t, and that the declaration of strcpy has no prototype; it names the header to include. The fix is to delete the hand declarations and include stdlib.h and string.h. On a 64-bit machine this matters: a function used without any declaration is assumed to return an int, which cannot hold a pointer.

$ gcc -std=c89 -pedantic -Wall -Wextra -o handdecl handdecl.c
handdecl.c:3:7: warning: conflicting types for built-in function ‘malloc’; expected ‘void *(long unsigned int)’ [-Wbuiltin-declaration-mismatch]
    3 | char *malloc();
      |       ^~~~~~
handdecl.c:2:1: note: ‘malloc’ is declared in header ‘<stdlib.h>’
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 | 
handdecl.c:4:7: warning: declaration of built-in function ‘strcpy’ without a prototype; expected ‘char *(char *, const char *)’ [-Wbuiltin-declaration-mismatch]
    4 | char *strcpy();
      |       ^~~~~~
$ ./handdecl
tide

void

void is the type that has no values. A function that returns void returns nothing, which K&R C expressed by returning an int that nobody used. A void * is a generic pointer, which K&R C expressed with char *.

voidfn.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void show(char *s)              /* returns nothing */
{
    printf("[%s]\n", s);
}

char *strsave(char *s)
{
    char *p;

    p = malloc(strlen(s) + 1);  /* void * needs no cast */
    if (p != NULL)
        strcpy(p, s);
    return p;
}

int main(void)
{
    char *s;

    s = strsave("harbour");
    show(s);
    free(s);                    /* free takes a void * too */
    return 0;
}

show returns void and needs no return statement. malloc returns a void *, which converts to any object pointer type without a cast, and free receives one. In the K&R programs of this series, the casts (struct node *) malloc(...) and free((char *) p) were needed; in ANSI C they can go.

$ gcc -std=c89 -pedantic -Wall -Wextra -o voidfn voidfn.c
$ ./voidfn
[harbour]

The PDP-11 compiler knows neither void nor parameter types in the parameter list. The first function definition already produces a cascade of errors, and the compiler gives up after a few lines.

$ cc voidfn.c
voidfn.c:5: Expression syntax
voidfn.c:6: External definition syntax
voidfn.c:7: s undefined; func. void
voidfn.c:10: Declaration syntax
voidfn.c:11: Declaration syntax

const

const marks an object that must not be changed after it is initialized. Its most useful place is in parameters: a pointer to const promises that the function will not change what it points to.

constant.c
#include <stdio.h>

/* const in the prototype: length does not change the string */
size_t length(const char *s)
{
    const char *p = s;

    while (*p != '\0')
        p++;
    return p - s;
}

int main(void)
{
    const double pi = 3.14159;
    const char *word = "tide";

    printf("%lu %g\n", (unsigned long) length(word), pi);
    return 0;
}

length takes a const char *, so it can be called with any string, and the compiler rejects an assignment through s or p inside it. pi cannot be assigned to after its initialization. The library functions are declared the same way: strcpy takes a const char * source. String literals are still arrays of plain char in C, although changing one is undefined; -Wwrite-strings makes GCC treat them as const, which finds the programs of the strings article that change a literal.

$ gcc -std=c89 -pedantic -Wall -Wextra -o constant constant.c
$ ./constant
4 3.14159

enum

An enumeration names a set of integer constants. It does the work of a group of #define lines, but the names belong to the language, not to the preprocessor, so a debugger can show them and the compiler can check them in a switch.

enums.c
#include <stdio.h>

enum colour { RED, GREEN, BLUE };
enum month { JAN = 1, FEB, MAR, DEC = 12 };

static const char *names[] = { "red", "green", "blue" };

int main(void)
{
    enum colour c;

    for (c = RED; c <= BLUE; c++)
        printf("%d %s\n", c, names[c]);
    printf("%d %d %d\n", JAN, MAR, DEC);
    return 0;
}

The constants of enum colour get the values 0, 1, and 2. A constant can be given a value, and the following ones count on from it: FEB is 2 and MAR 3. A variable of an enumeration type is an integer, so it can be compared and incremented.

$ gcc -std=c89 -pedantic -Wall -Wextra -o enums enums.c
$ ./enums
0 red
1 green
2 blue
1 3 12

Structures as values

In the book, a structure could not be assigned, passed to a function, or returned from one; programs copied members and passed pointers. ANSI C allows all three, and the structure is copied as a whole.

byvalue.c
#include <stdio.h>

struct point {
    int p_x;
    int p_y;
};

struct point makepoint(x, y)    /* returns a structure */
int x, y;
{
    struct point p;

    p.p_x = x;
    p.p_y = y;
    return p;
}

struct point addpoint(a, b)     /* receives two copies */
struct point a, b;
{
    a.p_x += b.p_x;
    a.p_y += b.p_y;
    return a;
}

int main()
{
    struct point a, b, c;

    a = makepoint(1, 2);
    b = a;                      /* structure assignment */
    b.p_x = 10;
    c = addpoint(a, b);
    printf("a = (%d, %d)\n", a.p_x, a.p_y);
    printf("b = (%d, %d)\n", b.p_x, b.p_y);
    printf("c = (%d, %d)\n", c.p_x, c.p_y);
    return 0;
}

makepoint returns a structure, b = a copies one, and addpoint receives copies of two structures and returns a third. Changing the copy b does not change a, and addpoint changes its own copy of a. The functions are still defined in the old style; the structure rules are independent of prototypes.

$ gcc -std=c89 -pedantic -Wall -Wextra -o byvalue byvalue.c
$ ./byvalue
a = (1, 2)
b = (10, 2)
c = (11, 4)

The V7 compiler already had these rules in 1979, a year after the book: structure assignment was one of the first changes to the language, and the book had announced it. The listing uses member prefixes, because the PDP-11 compiler shares member names between structures.

$ cc byvalue.c
$ a.out
a = (1, 2)
b = (10, 2)
c = (11, 4)

The C99 additions

C99 brought more changes that make old code look old: line comments, declarations after statements and in for loops, a 64-bit integer type, a boolean type, and integer types of fixed width.

modern.c
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

// a C99 program: none of this is K&R C
int main(void)
{
    long long big = 9000000000LL;
    uint16_t word = 65535;
    bool done = false;
    int n;

    for (int i = 0; i < 3; i++)
        word++;
    n = printf("%lld %u %d\n", big, (unsigned) word, done);
    int *p = &n;
    printf("%d characters, n at %p\n", n, (void *) p);
    return 0;
}

long long has at least 64 bits and its constants end with LL; uint16_t from stdint.h has exactly 16 bits and wraps from 65535 to 0; bool from stdbool.h holds true or false. i exists only in the loop, and p is declared after statements. The %p conversion of ANSI C prints a pointer, here an address that changes with every run, and printf returns the number of characters it wrote.

$ gcc -std=c99 -pedantic -Wall -Wextra -o modern modern.c
$ ./modern
9000000000 2 0
15 characters, n at 0x7fff66627b6c

The PDP-11 cannot compile any of it. It does not get past the preprocessor, which finds neither stdbool.h nor stdint.h.

$ cc modern.c
modern.c: 3: Can't find include file stdbool.h
modern.c: 4: Can't find include file stdint.h

A migration checklist

A K&R program is ported in small steps, compiling and testing after each one. The usual order is:

Source

Dennis Ritchie: The Development of the C Language

GCC 14: Porting to GCC 14 (implicit declarations)

GCC: Options to Request or Suppress Warnings

In this article we have ported K&R C to modern C: old code under the C99 standard, function prototypes and the calls they check, finding old-style code, the standard headers, void, const, enum, structures as values, the C99 additions, and a checklist for the work. The last article of the series covers debugging K&R C.

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.