Beware the implicit function declaration!

Eric Rapin rapin at bnrmtv.UUCP
Sat Nov 19 05:30:01 AEST 1988


Here is an interesting problem that I ran across:

#include        <stdio.h>

	int     a() {

		 (void)printf("a\n");
	}

	int     b() {

		  (void)printf("b\n");
	}

	int     c() {

		   (void)printf("c\n");
	}

	main() {

		int	a();
			b(),
			c();

		a();
	}

The output of this program is:
b
c
a

Not quite what you expect since we really only made a call to a().
Notice the semicolon after the declaration of a(). The following line
then becomes a call to b() and then a call to c(). Since both b() and
c() were declared to return int and they were not declared in main()
to return anything besides int, lint made no complaints about this
program. Unfortunately, I was trying to declare b() and c() explicitly
in main() and mistyped the semicolon.

This bring up an important point about "C" and that is that function
declarations and variable declarations are treated differently. I must
always declare my variables before I use them but as long as I don't
contradict with the return type of the function declaration of int,
then there is no requirement that my functions be explicitly declared
before I use them. Lint would have complained if b() and/or c() had
been declared as something other than int but it is a subtler point,
especially since many people never use lint.

Obviously this example is somewhat contrived but in a strongly typed
language (e.g. Eiffel) this would never happen. Function and variable
declarations are treated uniformly. Maybe a good suggestion to the
ANSI "C" committee would be to allow the programmer to turn off the
default int declaration of functions to force you to always declare
them explicitly. It would not break any existing software unless you
recompiled with this option on but would force new software to adhere
to stronger typing. But, then if I really want to do this sort of
thing then I shouldn't be programming in "C" :')

Please mail replies to me directly. If this creates a storm of
responses, I'll summarize.

Have fun,
-- 
Eric B. Rapin		     UUCP: {3comvax,amdahl,ames,csi,hplabs}!bnrmtv!rapin
Bell-Northern Research, Inc. ARPA: bnrmtv!rapin at ames.arpa
Mountain View, California



More information about the Comp.lang.c mailing list