C function calls: Understanding the “implicit int” rule

K&R2 covers the 1989/1990 version of the language. The current ISO C standard, published in 1999 2011, drops the “implicit int” rule, and requires a visible declaration for any function you call. Compilers don’t necessarily enforce this by default, but you should be able to request more stringent warnings — and you definitely should. In well-written new code, the rule is irrelevant (but it is necessary to understand it).

An example: the standard sqrt() function is declared in <math.h>:

double sqrt(double);

If you write a call without the required #include <math.h>:

double x = 64.0;
double y = sqrt(x);

a C90 compiler will assume that sqrt returns int — and it will generate code to convert the result from int to double. The result will be garbage, or perhaps a crash.

(You could manually declare sqrt yourself, but that’s the wrong solution.)

So don’t do that. Always include whatever header is required for any function you call. You might get away with calling an undeclared function if it does return int (and if your compiler doesn’t enforce strict C99 or C11 semantics, and if a few other conditions are satisfied), but there’s no good reason to do so.

Understanding the “implicit int” rule is still useful for understanding the behavior of old or poorly written code, but you should never depend on it in new code.

Leave a Comment