Re: Question regarding prototypes for 0-ary functions.



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?
.



Relevant Pages

  • Re: undefined reference to "..."
    ... compiler has a declaration of the function, ... extern int foo(int); ... Declarations appearing in the header files have ...
    (comp.lang.c)
  • Re: Strange function use
    ... int a, b, foo; ... int a, b, foo(int); ... the function prototype "foo" expects an integer argument ... foooccurs inside a declaration. ...
    (comp.lang.c)
  • Re: Forward declarations to static variables
    ... > extern int x; ... > static declaration of 'x' follows non-static declaration ... compiler could handle it inconsistently. ...
    (comp.lang.c)
  • Re: Newbie static class member question [C++]
    ... >and allows subsequent declarations to refer to Foo in certain contexts ... there is a name 'i' which designates an int ... In some contexts a declaration is also a definition. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Strange function use
    ... int a, b, foo; ... int a, b, foo(int); ... foooccurs inside a declaration. ... function prototype declaration. ...
    (comp.lang.c)