K&R C functions and old-style definitions
last modified December 5, 2026 K&R C 1978
A C program is a set of functions. In K&R C, parameters are named in the parameter list and typed in separate declarations, a function declaration says nothing about the parameters, and the compiler checks neither the number nor the types of the arguments. This article shows how K&R functions are defined, declared, and called, and what goes wrong when the declarations are missing.
An old-style definition
A K&R function definition starts with the return type, the function name, and the names of the parameters in parentheses. The declarations of the parameters follow on the next lines, before the opening brace of the body. This form is called an old-style or K&R-style definition.
#include <stdio.h>
int larger(a, b)
int a, b;
{
if (a > b)
return a;
return b;
}
int main()
{
printf("%d\n", larger(3, 8));
printf("%d\n", larger(-2, -7));
printf("%d\n", larger(larger(1, 9), 4));
return 0;
}
larger takes two int parameters and returns the
greater one. The line int a, b; declares the parameters;
without it they would default to int, and GCC would warn about
each of them. The return statement ends the function and gives
the value to the caller. A call can appear wherever a value can, even as an
argument of another call of the same function. The definition comes before
main, so the compiler knows the function when it reaches the
calls.
$ gcc -std=c89 -pedantic -Wall -Wextra -o larger larger.c $ ./larger 8 -2 9
Functions without a value
K&R C has no void. A function that is called only for its
effect is still an int function, and the caller ignores the
value. The book left out the return in such functions; the
series ends them with return 0;, since GCC warns when a
non-void function ends without a value.
#include <stdio.h>
int draw_line(n, c)
char c;
int n;
{
int i;
for (i = 0; i < n; i++)
printf("%c", c);
printf("\n");
return 0;
}
int main()
{
draw_line(12, '=');
printf("K&R C, 1978\n");
draw_line(12, '-');
return 0;
}
The parameter declarations need not follow the order of the parameter list:
c is declared before n. Each parameter that is not
an int must be declared. draw_line has a local
variable, i, declared at the start of the body. The calls in
main are statements of their own, and the returned 0 is thrown
away.
$ gcc -std=c89 -pedantic -Wall -Wextra -o draw_line draw_line.c $ ./draw_line ============ K&R C, 1978 ------------
Declaring a function
A function used before its definition should be declared. A K&R declaration gives only the return type and the name, followed by empty parentheses. It says nothing about the parameters.
#include <stdio.h>
int cube();
int main()
{
int i;
for (i = 1; i <= 5; i++)
printf("%d cubed is %d\n", i, cube(i));
return 0;
}
int cube(n)
int n;
{
return n * n * n;
}
The declaration int cube(); at the top of the file tells the
compiler that cube is a function that returns an
int. With it, main can come first, which is the
order the book prefers: the main program first, the helpers after it.
$ gcc -std=c89 -pedantic -Wall -Wextra -o cubes cubes.c $ ./cubes 1 cubed is 1 2 cubed is 8 3 cubed is 27 4 cubed is 64 5 cubed is 125
Implicit declarations
In K&R C, a function that is called without a declaration is declared
implicitly as a function returning int. The program compiles
and, for an int function, works. GCC accepts the rule in C89
mode but warns about it.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 3; i++)
printf("%d cubed is %d\n", i, cube(i));
return 0;
}
int cube(n)
int n;
{
return n * n * n;
}
The program is the previous one without the declaration. GCC reports the
implicit declaration and produces a working program, because
cube really returns an int. The book relied on this
rule and declared only the functions that returned other types.
$ gcc -std=c89 -pedantic -Wall -Wextra -o undeclared undeclared.c
undeclared.c: In function ‘main’:
undeclared.c:8:39: warning: implicit declaration of function ‘cube’ [-Wimplicit-function-declaration]
8 | printf("%d cubed is %d\n", i, cube(i));
| ^~~~
$ ./undeclared
1 cubed is 1
2 cubed is 8
3 cubed is 27
Functions that return other types
The implicit declaration fails for a function that returns anything other
than an int. The compiler assumes int at the call,
and then finds a definition with another return type.
#include <stdio.h>
int main()
{
printf("%f\n", half(5));
return 0;
}
double half(n)
int n;
{
return n / 2.0;
}
At the call, GCC assumes that half returns an int
and warns that the %f conversion does not match. At the
definition, the real return type is double, and the conflict is
an error. When the definition is in another file, the compiler never sees the
conflict; the caller then reads an int where the function left a
double, and the result is garbage.
$ gcc -std=c89 -pedantic -Wall -Wextra -o undeclared_double undeclared_double.c
undeclared_double.c: In function ‘main’:
undeclared_double.c:5:20: warning: implicit declaration of function ‘half’ [-Wimplicit-function-declaration]
5 | printf("%f\n", half(5));
| ^~~~
undeclared_double.c:5:14: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
5 | printf("%f\n", half(5));
| ~^ ~~~~~~~
| | |
| | int
| double
| %d
undeclared_double.c: At top level:
undeclared_double.c:9:8: error: conflicting types for ‘half’; have ‘double()’
9 | double half(n)
| ^~~~
undeclared_double.c:5:20: note: previous implicit declaration of ‘half’ with type ‘int()’
5 | printf("%f\n", half(5));
| ^~~~
Declaring non-int functions
A function that returns a type other than int must be declared
in every function or file that calls it before the definition. The book
places the declaration among the local declarations of the caller.
#include <stdio.h>
#define LOWER 0
#define UPPER 200
#define STEP 50
int main()
{
double celsius();
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr += STEP)
printf("%d\t%f\n", fahr, celsius(fahr));
return 0;
}
double celsius(fahr)
int fahr;
{
return (fahr - 32) * 5.0 / 9;
}
The declaration double celsius(); inside main is
valid for the rest of main. The function converts a temperature
in degrees Fahrenheit to Celsius. The constant 5.0 makes the
computation floating-point; with 5 the expression would be
computed in integers and lose its fraction. The parameter is an
int, the type of the argument: the declaration does not tell
the compiler the parameter type, so the argument is passed as it is, and a
double parameter would receive the wrong value.
$ gcc -std=c89 -pedantic -Wall -Wextra -o celsius celsius.c $ ./celsius 0 -17.777778 50 10.000000 100 37.777778 150 65.555556 200 93.333333
Hand-declared library functions
The book declared library functions by hand, like its own functions:
double sqrt(); before the first call of sqrt. GCC
knows the library functions it can compile inline, and it warns when a
hand-written declaration does not match the one in the standard headers.
#include <stdio.h>
double sqrt();
int strlen();
int main()
{
printf("%f\n", sqrt(2.0));
printf("%d\n", strlen("K&R C"));
return 0;
}
double sqrt(); and int strlen(); are the
declarations a K&R programmer writes. GCC compares them with its own
declarations, which list the parameter types and give strlen an
unsigned return type, and reports the differences. It even suggests the
header to include. The program still prints the right values. For GCC, the
series includes the headers math.h and string.h
instead.
$ gcc -std=c89 -pedantic -Wall -Wextra -o hand_decl hand_decl.c
hand_decl.c:3:8: warning: declaration of built-in function ‘sqrt’ without a prototype; expected ‘double(double)’ [-Wbuiltin-declaration-mismatch]
3 | double sqrt();
| ^~~~
hand_decl.c:4:5: warning: conflicting types for built-in function ‘strlen’; expected ‘long unsigned int(const char *)’ [-Wbuiltin-declaration-mismatch]
4 | int strlen();
| ^~~~~~
hand_decl.c:2:1: note: ‘strlen’ is declared in header ‘<string.h>’
1 | #include <stdio.h>
+++ |+#include <string.h>
2 |
$ ./hand_decl
1.414214
5
On V7 the hand-written declarations are simply the normal form; the system
has no string.h. The compiler accepts the program without a
word. The -lm option links the mathematics library, which holds
sqrt. When a program in this series includes a header that V7
lacks, its PDP-11 version declares the functions by hand instead.
$ cc hand_decl.c -lm $ a.out 1.414214 5
Call by value
Arguments are passed by value: the function receives copies of the arguments in its parameters. A parameter is a local variable that starts with the value of the argument, so a function can change its parameters without affecting the caller.
#include <stdio.h>
int power(base, n)
int base, n;
{
int p;
for (p = 1; n > 0; n--)
p = p * base;
return p;
}
int main()
{
int n;
n = 10;
printf("2 to the %d is %d\n", n, power(2, n));
printf("n is still %d\n", n);
printf("3 to the 4 is %d\n", power(3, 4));
return 0;
}
power counts its parameter n down to zero instead
of using a separate loop counter. Only the copy changes: after the call,
n in main is still 10. To change a variable of the
caller, a function needs its address; the
pointers article shows how.
$ gcc -std=c89 -pedantic -Wall -Wextra -o power power.c $ ./power 2 to the 10 is 1024 n is still 10 3 to the 4 is 81
No argument checking
Since a K&R declaration does not list the parameters, the compiler cannot check the arguments of a call. A call with the wrong number or wrong types of arguments compiles without a word, even with all warnings on.
#include <stdio.h>
int square(n)
int n;
{
return n * n;
}
int larger(a, b)
int a, b;
{
return a > b ? a : b;
}
int main()
{
printf("square(3) = %d\n", square(3));
printf("square(2.5) = %d\n", square(2.5));
printf("larger(7) = %d\n", larger(7));
printf("larger(1, 2, 3) = %d\n", larger(1, 2, 3));
return 0;
}
The program compiles without warnings, and only the first line is right.
square(2.5) passes a double where the function
expects an int; on x86-64 the two travel in different
registers, so square reads whatever its int
register holds. The number changed on every run. larger(7)
reads a second argument that was never passed, and
larger(1, 2, 3) silently ignores the third. This was the most
common class of bug in K&R programs. The
debugging article shows tools that find such
calls.
$ gcc -std=c89 -pedantic -Wall -Wextra -o no_checking no_checking.c $ ./no_checking square(3) = 9 square(2.5) = 498911488 larger(7) = 4202536 larger(1, 2, 3) = 2
On the PDP-11 all arguments travel on the stack, and the garbage is different
but the same on every run. square(2.5) pushes the four 16-bit
words of a double; square takes the first word as
its int and squares it. larger(7) compares 7 with
whatever word lies next on the stack, and here 7 wins.
$ cc no_checking.c $ a.out square(3) = 9 square(2.5) = 17408 larger(7) = 7 larger(1, 2, 3) = 2
Argument promotions
Without prototypes, every argument goes through the default argument
promotions: a char or short is passed as an
int, and a float as a double. A
function with a char or float parameter receives
the promoted value and converts it back.
#include <stdio.h>
int show(c, f)
char c;
float f;
{
printf("c = %c, f = %f\n", c, f);
return 0;
}
int main()
{
char letter;
float price;
letter = 'x';
price = 2.5;
show(letter, price);
show('y', 1.25);
show(65, 3);
return 0;
}
The first two calls work: letter and 'y' arrive as
int values, and price and 1.25 as
double values, which is what show expects. In the
third call 65 is an int and is converted to the character
A, but 3 is also an int, not a
double, so it is not converted. f receives
whatever was in its floating-point register, here the 1.25 left over from
the previous call. A K&R programmer writes 3.0, or
(double) n for a variable.
$ gcc -std=c89 -pedantic -Wall -Wextra -o promotions promotions.c $ ./promotions c = x, f = 2.500000 c = y, f = 1.250000 c = A, f = 1.250000
On the PDP-11, show takes four words from the stack for
f: the int 3 and the three words after it. Read as
a double, these bits are 0.
$ cc promotions.c $ a.out c = x, f = 2.500000 c = y, f = 1.250000 c = A, f = 0.000000
Recursion
A C function may call itself, directly or indirectly. Each call gets its own copy of the parameters and local variables. The book prints a number digit by digit with a recursive function, and this program does the same.
#include <stdio.h>
int print_number(n)
int n;
{
if (n < 0) {
printf("-");
n = -n;
}
if (n / 10)
print_number(n / 10);
printf("%c", n % 10 + '0');
return 0;
}
int gcd(a, b)
int a, b;
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
print_number(1978);
printf("\n");
print_number(-305);
printf("\n");
printf("gcd(84, 36) = %d\n", gcd(84, 36));
return 0;
}
print_number prints a minus sign for a negative number and then
lets a recursive call print all digits but the last before it prints the
last digit itself. This puts the digits in the right order, which the
do loop in the control flow
article could not do. n % 10 + '0' turns a digit into its
character. gcd computes the greatest common divisor with
Euclid's algorithm. The book's recursive quicksort appears in the
sorting article.
$ gcc -std=c89 -pedantic -Wall -Wextra -o recursion recursion.c $ ./recursion 1978 -305 gcd(84, 36) = 12
Source
Dennis Ritchie: C Reference Manual (1975)
GCC: Options to Request or Suppress Warnings
GCC 14: Porting to GCC 14 (implicit declarations)
In this article we have defined K&R functions with old-style parameter
declarations, declared them before use, seen what implicit declarations do
to int and double functions, and found the calls
that the compiler cannot check. The next article,
K&R C scope and storage classes, covers local
and external variables. Modern C functions with prototypes are described in
C functions.
Author
List all K&R C tutorials.