declaration query

david.f.prosser dfp at cbnewsl.ATT.COM
Tue Jun 20 23:34:13 AEST 1989


In article <14587 at watdragon.waterloo.edu> afscian at violet.waterloo.edu (Anthony Scian) writes:
>This question arose from comp.lang.c:
>
>Is this legal (ANSI)?
>int foo( int, int y ) { ... }			(1)

No, but it would have been nice if it were valid, in my opinion.

>
>I think this should be legal:
>int foo( int, int y );

Yes, it is valid.

>
>and combined with this:
>int foo( int x, int ) {return(x+y);}
>
>COULD be equivalent to:
>int foo( int x, int y ) {return(x+y);}

No.  The scope of prototype parameters in a declaration ends at the
close paren.

>
>The grammar allows this but it is not clear to me what (1) means
>in isolation.

Nothing, since it is invalid.  An (obvious, at least to me) interpretation
is to have an int parameter whose value is not accessible in the function.
It would be a more precise (and linguistic) method of noting that a
parameter which is needed for other reasons is unused in a function.  One
example is while a program is being developed: the stub functions could
be implemented without drawing comments about unused parameters.  Another
is when a number of functions are called indirectly.  All these functions
must have identical interfaces for portability, but some of the functions
may well not need the value of certain parameters.

>Can identifiers in prototypes be different than the identifiers 
>in the function definition? 

Yes, since the scope of the names are completely disjoint.

>What about?
>int test( int, int z );
>int test( int x, int y ) {return(x+y);}

Valid.

>
>Should the compiler keep the identifiers and override them when
>the function is defined (declared)?

No.  I see no benefits gained by producing warning messages about
differently named parameters between a declaration (probably in a
header file) and the definition.  In fact, the only use for naming
parameters in a declaration is to provide a little documentation
that might help to distinguish between the parameters:

	div_t div(int numerator, int denominator);

>
>//// Anthony Scian afscian at violet.uwaterloo.ca afscian at violet.waterloo.edu ////
>"I can't believe the news today, I can't close my eyes and make it go away" -U2

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.std.c mailing list