0

So I was recently reading a bit on Hacker News about function pointers and was enlightened to the fact that void foo() and void foo(void) are NOT equivalent prototypes. So, I set about ensuring that this is actually true:

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

Sure enough, this code compiles without even so much as a warning.. where as this code will throw an error:

int foo(void)
{
  return 0;
}
int main()
{
  return foo(1,2,3,4);
}

This seems very error prone. I also thought that ... for "any amount of arguments", such as in printf's signature

int printf ( const char * format, ... );

Was this also true in C89 or K&R? Can anyone give insight into the use case for this "feature"?

Earlz
  • 62,085
  • 98
  • 303
  • 499

1 Answers1

1

It's not really a "feature", per se. It's just the way the language used to be back in the beginning, so the syntax has lived on to keep old code working. The addition of void made it possible to have functions that explicitly took no arguments, for example.

The use of the ... indicates a variadic function, which is subtly different from a function that just takes an arbitrary number of arguments, though. Using the ... requires the use of stdarg.h macros, but just having a function declared with () doesn't.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469