0

I know that that there's a difference in the following two functions declarations:

int foo1();
int foo2(void);

because that says foo1() can accept any number of arguments while foo2() accepts no arguments.

But for the implementation, does the C language spec prefer one of the following?

int foo2() { return 2; }
int foo2(void) { return 2; }

In other words, is it appropriate to include void in the otherwise empty parameter list?

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • Your *implementations* of `foo2()` are equivalent. The first, however, does not provide a prototype **to the compiler** making its use more problematic. – pmg Mar 03 '20 at 20:17
  • 1
    I am not sure the marked duplicate if answering this question. The answer there is not addressing the definition `int foo()` *with* a prototype `int foo(void)`. Or maybe it is not the question? – Eugene Sh. Mar 03 '20 at 20:19
  • A function without a prototype is a regular function that accepts a specific number of arguments, each with its specific type. However that information is not available to the compiler, the programmer has to get it right (or suffer UB). A function with `...` accepts a variable number of arguments, with varying types. – pmg Mar 03 '20 at 20:19
  • The marked duplicate answers this question completely. Whether `int foo()` is preferable to `int foo(void)` depends on whether or not you believe the compiler should *check for the absence of arguments in function calls.* – Robert Harvey Mar 03 '20 at 20:25
  • The marked duplicate makes it sound like `int foo() {return 23;}` _should_ tell the compiler to reject arguments in subsequent calls to foo, but all the compilers I can conveniently test do _not_ reject arguments in subsequent calls to foo. This might just mean that the accepted answer to the duplicate is wrong, though. – zwol Mar 03 '20 at 20:55
  • @RobertHarvey: I read the marked duplicate and it does not answer what happens with `void foo(void);` declaration followed by `void foo() {...}` definition. Do you agree? – fearless_fool Mar 03 '20 at 23:23
  • The `void foo(void)` fails to compile if you try to call it with any arguments. If you try that with `void foo()`, it will compile and then produce undefined behavior. – Robert Harvey Mar 03 '20 at 23:25
  • Note that some compilers will warn you, but then produce the correct behavior anyway. See https://repl.it/@robertwharvey/Empty-parameter-lists-in-C – Robert Harvey Mar 03 '20 at 23:27
  • @RobertHarvey I specifically wasn't asking about _calling_ the function. Just about the definition. The marked duplicate doesn't address that. Methinks it's time to re-ask the improved question. (You can vote it down! :) – fearless_fool Mar 03 '20 at 23:27
  • The definition is of no consequence unless you actually use the function by *calling* it. – Robert Harvey Mar 03 '20 at 23:28
  • I vote to close this question in favor of https://stackoverflow.com/questions/60517172/definition-of-int-foo-vs-int-foovoid-following-declaration-of-int – fearless_fool Mar 03 '20 at 23:48

0 Answers0