Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't we use (void) in main?

Tags:

c

People also ask

Why void is not used in main in C++?

Why can't I use void main in c++ Because in C++ main method always return 0. That's why. It's predefined.

Can you do void main?

No. It's non-standard. The standard prototype of main is int main() with the optional command line arguments argc and argv . The int returned by main() is a way for a program to return a value to the system that invokes it.

Can you put void in int main?

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.

Can Main have void return type?

C++ does not allow main to have a void return type. The published C++ standard requires it to be int . Some C++ compilers allow you to use void , but that's not recommended.


I'm not sure what the standards are nowadays, but in traditional ANSI C, using empty parentheses indicates that the function can take any number of arguments. Declaring a void parameter on the other hand indicates that the function only takes zero arguments. In this case (and many others), it really doesn't matter too much.

If you want to be strict though, it's probably best to define the void parameter. Of course, the main function can also be defined as int main(int argc, const char* argv[]) - which is perfectly valid, but often unnecessary if you don't care about arguments.


From the C99 standard:

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

When main is defined without parameters, will argc and argv still be present on the stack?


These prototypes of main() are both non-standard.

Precision on that question can be found on the comp.lang.c faq : http://c-faq.com/decl/main.html

EDIT: changed "wrong" to "non-standard" as the norm allows implementation-defined prototypes.


There is no difference but usually main should return int. Some compilers will give you a warning (at least the GNU compiler - gcc):

$ cat x.c
void main(void){}

$ gcc x.c
x.c: In function `main':
x.c:1: warning: return type of 'main' is not `int'

As mentioned the prototype of main is (according to standard):

int main(int argc, const char* argv[])


main is a function, as other function. Almost. Anyway, being a function, it is called by some other code (a start up code). Usually (read: almost always) int main() is the correct one, but indeed what is the real correct one depends on the platform you are working it. Since, as said, main function could be called by a startup code that pass in no arguments at all, and that expect no a return value in a specific register (so that void main(void) is correct).

The int main() is correct since normally start up code expect a return value, and pass in two arguments. By saying int main(void) you are saying main takes no argument at all, that is false in most cases. With () you say there are arguments (one, two, three, you don't care), but you are not interested in them, so you are not interested in saying what they are and which type they are.

As I can see in codes, the most used prototype for "normal" environments (no embedded device or other "strange" environments where main can be called differently) is int main() when you disregard the passed int argc, char **argv arguments. (GCC complain since we are using a version for gcc suitable for the enviroment; test it with cross GCC version for one of the environment where startup code does not pass any arguments and expect no a return value)

edit

Just to be kind to skeptical persons; on the an environment where the main function is called, with two arguments, the following

int func()
{
  return 0;
}

int func2(void)
{
  return 1;
}

int main(void)
{
  int a;
  a = func(a, a); /* A */
  a = func2(a);   /* B */
  return 0;
}

says no error for A, while for B says too many arguments to function ‘func2’, compiled with gcc -std=c99 -pedantic. Changing int main(void) into int main() makes no difference, and no warnings.

On other evironments (I can't do practical tests now), void main(void) is ok, while in this case it raises a warning. The warning is not because of standard alone, but only since in the environment in use the prototype for main does not match. Standard seems to allow any other "configuration" for main.

In the OP case, considerering the "normal" enviroment (O.S. like GNU/Linux e.g.), where two args are passed to the main, and a return value is expected, the int main() is preferable (arguments are pushed on the stack by the startup code whether you say int main(void) or not, so int main() to me make more sense)

edit

One more note, always for skeptical person. As already proved, B raises an error, since I've said that it is int func2(void) but I call it passing an argument. Then, let us suppose we can compile the startup code and link it, as any other code. Somewhere, it will call the main, in a way like

retval = main(argc, argv);

If we used int main(void), the compiler will stop, giving an error, since startup code (in this environment) is trying to call main with two arguments. If we use int main() nothing happens and the code gets compiled correctly.

So, int main() is superior to int main(void) (in environment where we expect two arguments to main possible)

edit

More likely the call is like

retval = main(_argc, _argv, environ);

on many systems, but this does not change the previous speech.

final edit

Did anyone find that when building a command line tool (i.e. on systems where int argc, char ** makes sense) with int main(void), the chosen compiler/linker links a startup code where the main is called without arguments (whatever the calling conventions are), and instead when building with int main(int argc, char **argv) the startup code is different and in fact calls the main with those two arguments (even if the main itself doesn't use them)?