Func Protos with K&R Func Defs

David E. Smyth david at jpl-devvax.JPL.NASA.GOV
Fri Mar 1 05:46:37 AEST 1991


jik at athena.mit.edu (Jonathan I. Kamens) writes:
>
>  In ANSI C, a function declaration must match its definition.  That means
>that if the declaration is prototyped, then the definition must also be
>prototyped, with the same types; if the declaration is not prototyped, then
>the definition cannot be prototyped either.
>
>  This isn't just a matter of being picky, either; the differences between how
>prototyped and unprototyped functions are compiled will cause your programs to
>break if your declarations don't match your definitions.  That's why the file
>that defines functions should always include the header file that declares
>them, so you'll get errors if the declaration and the definition don't match.

I always include the file which has the prototyped declarations in the
file which has the K&R style definitions.  You are correct, the following
does not compile:

----- random.h -----
extern void random(char a, char b, char c, char d, char e);
----- random.c -----
#include "random.h"

void random(a, b, c, d, e)
char a, b, c, d, e;
{
    printf("%c %c %c %c %c\n", a, b, c, d, e);
}
----- main.c -----
#include "random.h"

void main()
{
    random('a', 'b', 'c', 'd', 'e');
}
----- how I tried compiling it -----
bugs:david <34> gcc -ansi main.c random.c -o ran
random.c: In function random:
random.c:5: argument `a' doesn't match function prototype
random.c:5: a formal parameter type that promotes to `int'
random.c:5: can match only `int' in the prototype
random.c:5: argument `b' doesn't match function prototype
random.c:5: a formal parameter type that promotes to `int'
random.c:5: can match only `int' in the prototype
random.c:5: argument `c' doesn't match function prototype
random.c:5: a formal parameter type that promotes to `int'
random.c:5: can match only `int' in the prototype
random.c:5: argument `d' doesn't match function prototype
random.c:5: a formal parameter type that promotes to `int'
random.c:5: can match only `int' in the prototype
random.c:5: argument `e' doesn't match function prototype
random.c:5: a formal parameter type that promotes to `int'
random.c:5: can match only `int' in the prototype

Bleech!  I stand corrected - I see I do have to use that
horribly ugly programming style of having both the
declarations in include files and the definitions in
the source files BOTH enclosed by #ifdefs

Bleech!  Gag!  *&@#$^ &*#Q$ &@*#$ @*&#$ !!!

-------------------------------------------------------------------------
David Smyth				david at jpl-devvax.jpl.nasa.gov
Senior Software Engineer,		seismo!cit-vax!jpl-devvax!david
X and Object Guru.			(818)393-0983
Jet Propulsion Lab, M/S 230-103, 4800 Oak Grove Drive, Pasadena, CA 91109
------------------------------------------------------------------------- 
	One of these days I'm gonna learn:  Everytime I throw
	money at a problem to make it disappear, the only thing
	that disappears is my money...
-------------------------------------------------------------------------



More information about the Comp.lang.c mailing list