7

Possible Duplicate:
C void arguments

Just started with C and I can't find answer to this...

Is there any difference between

int foo() { }

int foo(void) { }

Which should I prefer and why?

Note that this question also goes for: int main. Should it be: int main or int main(void) when I don't want any command line arguments.

Community
  • 1
  • 1
Kanishk
  • 225
  • 1
  • 3
  • 6
  • 3
    Similar questions: [Is there a difference between foo(void) and foo() in C++ or C](http://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c) and [C void arguments](http://stackoverflow.com/questions/693788/c-void-arguments) – Devendra D. Chavan Feb 22 '11 at 08:03

6 Answers6

15

The two canonical forms of main are, according to the standard (see C99 section 5.1.2.2.2 "Program startup"):

int main (void);
int main (int argc, char *argv[]); // or equivalent such as char **argv

Others are specifically allowed but those are the required ones.

As to the preferred form between fn(void) and fn(), I prefer the former since I like to explicitly state that there are no parameters.

There is also a subtle difference. C99 section 6.7.5.3 "Function declarators", paragraph 10, states:

The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

Paragraph 14 of that same section shows the only difference:

An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

In other words, it means the same as void in the function definition but does not mean that in a standalone declarator (i.e., the prototype). int fn(); means that no information on the parameters is yet known but int fn(void); means there are no parameters.

That means that:

int fn();
int fn (int x) { return x; }
int main (void) { return fn(0); }

is valid but:

int fn(void);
int fn (int x) { return x; }
int main (void) { return fn(0); }

is not.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • Thanks for your answer. Where can I download the standard from just for reference? I seem to be hitting paid links while searching. – Kanishk Feb 22 '11 at 08:20
  • The best you can do for free is probably at the bottom of the wikipedia page http://en.wikipedia.org/wiki/C99 or a direct link http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf - this is the draft of C99 + TCs 1, 2 and 3. It's pretty dry reading though :-) – paxdiablo Feb 22 '11 at 08:25
  • Thank you for an awesome answer. – Kanishk Feb 22 '11 at 08:27
4

In addition to paxdiablo's reply, which covers the C language and thus the OP's question, it may be worth noting that "void style" is considered bad practice in C++, at least by Bjarne Stroustrup.

The reasons why are purely style-related, but the use of void as function parameter was actually banned from C++ in the early days before ISO standardization. The void syntax was eventually allowed though, to provide compatibility with C.

So to sum this up:

  • In C programs, use f(void) because it enforces stricter typing. (rational argument)
  • In C++ programs, use f(), or you will upset various C++ gurus. (irrational argument)
Nayuki
  • 16,655
  • 5
  • 47
  • 75
Lundin
  • 155,020
  • 33
  • 213
  • 341
1

Nope, no difference. Some old C compilers would complain about the absence of void in that case, but there is no reason to do that in modern C or C++.

Ed S.
  • 115,705
  • 20
  • 165
  • 244
  • Which one should I prefer? Surely there has to be the one everyone uses! I even downloaded the `coreutils` source code to check, but every `main` has a command line argument! – Kanishk Feb 22 '11 at 08:03
  • Not true. See http://stackoverflow.com/questions/416345/is-fvoid-deprecated-in-modern-c-and-c – phord Mar 19 '12 at 16:21
0

there are no differeces between the statements.

If you write int main(void), this statement tells the compiler that main does not contain any arguments.

And similarly, in case of main() or int main(), again the compiler understands main does not contain any arguments. But, in the first case, you are forcefully telling the compiler, while in second statement the compiler understand automatically.

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
Manish Trivedi
  • 3,165
  • 4
  • 21
  • 27
  • Not true. foo(void) means the function takes zero arguments. foo() means the function takes an unknown number of arguments. See http://stackoverflow.com/questions/416345/is-fvoid-deprecated-in-modern-c-and-c – phord Mar 19 '12 at 16:20
0

Whether or not you use command line arguments the signature of main() includes the parameters that would allow command line arguments to be accessed.

View the signature as a contract with the caller, not an announcement of what the implementation actually uses.

djna
  • 52,574
  • 11
  • 70
  • 109
-1

They are the same, but preferred one is int foo() { }

Jaanus
  • 15,013
  • 43
  • 137
  • 193
  • Why is that preferred? It allows you to pass an object of type Rubbish_t to the function foo(). – Lundin Feb 22 '11 at 09:55
  • They are not the same to a C compiler. foo(void) means the function takes zero arguments. foo() means the function takes an unknown number of arguments. – phord Mar 19 '12 at 16:18