[comp.lang.c] legal ANSI prototype?

brianh@hpcvia.CV.HP.COM (brian_helterline) (08/29/90)

Hello netland,

I have a question regarding the ANSI interpretation of a prototype
declaration.  My compiler accepts it, but a pc-lint program does not.
I am interested in knowing which one is wrong.  Here it is:

extern intSomeFunction( int );

int main( void );
int main( void )
{
	int i = SomeFunction( 5 );
	return 0;
}

MSC takes this just fine but my pc-lint complains that SomeFunction
is called without a prototype being in scope.  If I add a space in the
prototype, then everybody is happy.  My question is
	1) is that prototype legal ANSI?
	2) Am I just lucky that MSC accepts it?
	3) Is my pc-lint broken because it misses it?

As to the reason why it is like that, some software I purchased the
following for prototypes:

#define IMPORT( t )	extern t

IMPORT( int )SomeFunction( int );
....

Thanks for your help.

-Brian

eager@ringworld.Eng.Sun.COM (Michael J. Eager) (08/29/90)

In article <31530015@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
>Hello netland,
>
>I have a question regarding the ANSI interpretation of a prototype
>declaration.  My compiler accepts it, but a pc-lint program does not.
>I am interested in knowing which one is wrong.  Here it is:
>
>extern intSomeFunction( int );
>
>int main( void );
>int main( void )
>{
>	int i = SomeFunction( 5 );
>	return 0;
>}
>
>MSC takes this just fine but my pc-lint complains that SomeFunction
>is called without a prototype being in scope.  If I add a space in the
>prototype, then everybody is happy.  My question is
>	1) is that prototype legal ANSI?

The prototype is legal: the function name is intSomeFunction.  This
function is never referenced.  It's not clear that this is what you
intended.


>	2) Am I just lucky that MSC accepts it?

No luck involved.  MSC says that you have a call to SomeFunction which
is not declared so it gets a K&R style default declaration: it is assumed 
to return an integer and there is no checking on argument number or type.
This is the ANSI interpretation of undeclared functions.


>	3) Is my pc-lint broken because it misses it?

Misses what?  You write that lint complains that SomeFunction does not
have a declaration, which is the case.  Lint is warning you that you 
may have misspelled the name in a declaration, which is the case.  

>
>As to the reason why it is like that, some software I purchased the
>following for prototypes:
>
>#define IMPORT( t )	extern t
>
>IMPORT( int )SomeFunction( int );
>....

The preprocessor is supposed to insert whitespace after the expansion
of a macro.  If it doesn't and the define is as described, there seems
to be a preprocessor bug.  


-- Michael Eager

brianh@hpcvia.CV.HP.COM (brian_helterline) (08/30/90)

>I have a question regarding the ANSI interpretation of a prototype
>declaration.  My compiler accepts it, but a pc-lint program does not.
>I am interested in knowing which one is wrong.  Here it is:

>extern intSomeFunction( int );

>int main( void );
>int main( void )
>{
>	int i = SomeFunction( 5 );
>	return 0;
>}

>MSC takes this just fine but my pc-lint complains that SomeFunction
>is called without a prototype being in scope.  If I add a space in the
>prototype, then everybody is happy.  My question is
>	1) is that prototype legal ANSI?
>	2) Am I just lucky that MSC accepts it?
>	3) Is my pc-lint broken because it misses it?

>As to the reason why it is like that, some software I purchased the
>following for prototypes:

>#define IMPORT( t )	extern t

>IMPORT( int )SomeFunction( int );
>....

	First of all, thanks to all who responded to my note.  I have
	since figured it exactly what is going on. 

	When MSC preprocesses the IMPORT( int )SomeFunction( int ), it
	does add a space to get extern int SomeFunction( int ) _BUT_ if
	you preprocess to a file, the space is _NOT_ there and your
	preprocess file is no longer equivalent to your source code.

	My pc-lint program _DOES NOT_ add the space when it expands the
	macro so the function prototype is different that what it
	should be with ANSI C.

	As a side question, if you preprocess a source file to another
	file, is that file required to compile the same as the source
	file?  I remember seeing some discussion on the net about it
	but I can't remember.