[comp.lang.c] Beware the implicit function declaration!

rapin@bnrmtv.UUCP (Eric Rapin) (11/19/88)

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@ames.arpa
Mountain View, California

gwyn@smoke.BRL.MIL (Doug Gwyn ) (11/20/88)

In article <4264@bnrmtv.UUCP> rapin@bnrmtv.UUCP (Eric Rapin) writes:
>... 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.

I'm posting my response (which should not be taken as an official
X3J11 position) because it may be of general interest:

X3J11 (the ANSI C committee) didn't want to try to dictate any
details of how C compilers are invoked, and they didn't want to
specify any form of multiple "levels" of the standard.  Vendors
will in many cases provide a variety of options, but only the
standard-conforming mode of operation will be covered by the
ANSI/ISO C standard specification.  You can try to convince your
compiler vendor that the option you suggest is a good idea..

jrll@Portia.Stanford.EDU (john ralls) (11/20/88)

Unless I misunderstand completely the rules on declaration, simply by
defining the functions before main(), they were also declared.  C
defaults to an int return value, so as long as he didn't try to assign
the return value to something else, he got no warnings.  Incidentally,
since these functions niether take nor return any value, they should be
prototyped or declared as void a(void).  This can be done in the body of
main() iff main() is the first function in the file or is in another
file (in which case it should be extern void a (void)) (this is
prototyping);  otherwise the start of the definition should be the
declaration as well.
John