Visual C accepts the wrong number of arguments?

Why does this compile in Visual Studio:

void foo(int a) {}

int main() {
    foo(1,2);
}

      

There is a warning

source_file.c(4) : warning C4020: 'foo' : too many actual parameters

      

but why is it not an error, as is the case with gcc / clang?

I know the definitions of K & R-style functions, but this only applies to foo()

one that takes a variable number of arguments.

Quotes from a standard allowing this would be greatly appreciated.

+3


source to share


1 answer


It's not just MSVC.

GCC accepts it if your function definition is below the calling site and there is no prototype. C has always allowed invoking an undeclared function. It passes the prototype from the calling site. So I think the behavior is related to this aspect (although when I move the function over the correspondent site in GCC it changes to an error, which makes sense for C99). It should be Undefined Behavior, though (different number of arguments than parameters).

int main()
{
   foo(1,2,3);
}

void foo(int a, int b)
{
}

f.c:6:6: warning: conflicting types for ‘foo’ [enabled by default]
 void foo(int a, int b)
      ^
f.c:3:4: note: previous implicit declaration of ‘foo’ was here
    foo(1,2,3);

      

I found this

6.7.5.3p15:



[...] If one type has a parameter type list and the other type is given by a function definition that contains a (possibly empty) list of identifiers [this is your situation], both agree on the number of parameters and the type of each prototype parameter must be compatible with the type that results from the default declaration for type arguments of the corresponding identifier. [...]

.... but this paragraph is not part of the limitation. The violation "must" is outside the Undefined section of the constraint and should not be diagnosed (4p2).

I quoted this: http://compgroups.net/comp.lang.c/why-is-this-not-an-error-in-visual-c/732881

Your mileage may vary.

In short, apparently the only requirement is that the compiler barks at you for some definition of the bark. In VS 2013, it is seen as a bug.

As far as the argument goes, for the same reason that variable argument arguments work in C, the calling site must invoke an additional argument, but the callee won't know about it (just guessing here). While this works, it does not mean that this behavior is defined.

+3


source







All Articles