Re: Question regarding prototypes for 0-ary functions.
- From: Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx>
- Date: 21 Dec 2008 00:28:48 -0800
Harald van =?UTF-8?b?RMSzaw==?= <truedfx@xxxxxxxxx> writes:
On Tue, 02 Dec 2008 17:31:06 -0800, Tim Rentsch wrote:
This discussion got me to thinking - what's the intended behavior for
the two file program below?
foo.c
-----
int foo(x) int x; { return x; }
bas.c
-----
#include <stdio.h>
extern int foo( int, ... );
int main( void ){
printf( "foo(1) = %d\n", foo( 1 ) );
return 0;
}
Strictly conforming program, or undefined behavior?
Hint: consulting 6.7.5.3 fairly carefully might be a good idea here...
I suspect you're referring to:
If one type has a parameter type list and the other type is specified
by a function declarator that is not part of a function definition
and that contains an empty identifier list, the parameter list shall
not have an ellipsis terminator and the type of each parameter shall
be compatible with the type that results from the application of the
default argument promotions.
This says that if foo is defined as int foo(int x, ...) { ... }, then
extern int foo(); is not a valid declaration, but not the other way
around.
If one type has a parameter type list and the other type is speci�ed
by a function de�nition that contains a (possibly empty) identi�er
list, both shall agree in the number of parameters, and the type of
each prototype parameter shall be compatible with the type that
results from the application of the default argument promotions to
the type of the corresponding identi�er.
The definition of foo says it takes one parameter, while the declaration
says it takes a variable number of arguments. Are you making a (correct)
distinction between parameter and argument here -- which seems like an
oversight to me -- or did you mean something else?
The word 'argument' is correct, part of the compound term 'default
argument promotions', from 6.5.2.2 p 6.
I read both the definition and the declaration as having one
parameter, per 3.15
parameter
formal parameter
formal argument (deprecated)
object declared as part of a function declaration or definition that
acquires a value on entry to the function, or an identifier from the
comma-separated list bounded by the parentheses immediately following
the macro name in a function-like macro definition
The definition for 'foo' declares one object (i.e., variable), and the
declaration for 'foo' declares one object. So, by the Standard's
terminology, each has one parameter.
Since the number of parameters matches, and the other conditions of
6.7.5.3 p 15 are met, the types are compatible, and the function call
should be allowed under the conditions given in 6.5.2.2 (in particular
paragraphs 7 and 9). Right?
.
- Follow-Ups:
- Re: Question regarding prototypes for 0-ary functions.
- From: Harald van Dijk
- Re: Question regarding prototypes for 0-ary functions.
- References:
- Re: Question regarding prototypes for 0-ary functions.
- From: Tim Rentsch
- Re: Question regarding prototypes for 0-ary functions.
- From: Harald van Dijk
- Re: Question regarding prototypes for 0-ary functions.
- Prev by Date: Re: obfiscated code
- Next by Date: Re: Question regarding prototypes for 0-ary functions.
- Previous by thread: Re: Question regarding prototypes for 0-ary functions.
- Next by thread: Re: Question regarding prototypes for 0-ary functions.
- Index(es):
Relevant Pages
|