![]() |
VOOZH | about |
Note: This was true for older versions of C but has been changed in C11 (and newer versions). In newer versions, foo() is same as foo(void).
Consider the following two definitions of main().
Definition 1:
Definition 2:
In C++, there is no difference, both are the same. Both definitions work in C also, but the second definition with the void is considered technically better as it clearly specifies that the main can only be called without any parameter.
In C, if a function signature doesn't specify any argument, it means that the function can be called with any number of parameters or without any parameters. For example, try to compile and run the following two C programs (remember to save your files as .c). Note the difference between the two signatures of fun().
Output of C code
( no output )
Output of C++ code
./Solution.cpp: In function 'int main()':
./Solution.cpp:9:24: error: too many arguments to function 'void fun()'
fun(10, "GfG", "GQ");
^
./Solution.cpp:5:6: note: declared here
void fun() {}
The above program compiles and runs fine in C, but the following program fails in compilation.
Output of C/C++ Code:
prog.cpp: In function ‘int main()’:
prog.cpp:8:23: error: too many arguments to function ‘void fun()’
fun(10, "GfG","GQ");
^
prog.cpp:4:6: note: declared here
void fun(void) { }
^
Unlike the last example, this example fails in compilation in both C and C++.
From the above, we can conclude that,
In C++, both fun() and fun(void) are same.
So the difference is, in C, int main() can be called with any number of arguments, but int main(void) can only be called without any argument. Although it doesn't make any difference most of the times, using "int main(void)" is a recommended practice in C.
Answer:
Answer: