1.

Solve : int main() vs int main(void)?

Answer»

Got into a discussion at work with another C++ hobbyist and they looked at a program I wrote and asked why not use

int main(void) instead of int main()

I personally have always used just int main() for the most part because its by habit and how I was taught in college.

Looking here there is a discussion on it:
http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c

Quote

Standard C++

The C++11 standard (ISO/IEC 14882:2011) says:

3.6.1 Main function [basic.start.main]

¶1 A program shall contain a global function called main, which is the designated start of the program. [...]

¶2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation defined. All implementations shall allow both of the following definitions of main:

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

and

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

In the latter FORM argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as POINTERS to the initial characters of null-terminated multibyte strings (NTMBSs) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be non-negative. The value of argv[argc] shall be 0. [ Note: It is recommended that any further (optional) parameters be added after argv. —end note ]

¶3 The function main shall not be used within a program. [...]

¶5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the ARGUMENT. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;


And the int main(void) seems to be dated back to earlier C++ standards



Quote
The accepted answer appears to be targetted for C++, so I thought I'd add an answer that pertains to C, and this differs in a few ways.

ISO/IEC 9899:1989 (C90):

main should be declared as either:

int main(void)
int main(int argc, char **argv)

Or equivalent. For example, int main(int argc, char *argv[]) is equivalent to the second one. Further, the int return type can be omitted as it is a default.

If an implementation permits it, main can be declared in other ways, but this makes the program implementation defined, and no longer strictly conforming.

The standard defines 3 values for RETURNING that are strictly conforming (that is, does not RELY on implementation defined behaviour): 0 and EXIT_SUCCESS for a successful termination, and EXIT_FAILURE for an unsuccessful termination. Any other values are non-standard and implementation defined. main must have an explicit return statement at the end to avoid undefined behaviour.

Finally, there is nothing wrong from a standards point of view with calling main() from a program.

ISO/IEC 9899:1999 (C99):

For C99, everything is the same as above except:

The int return type may not be omitted.
You may omit the return statement from main. If you do, and main finished, there is an implicit return 0.


So does it really not matter int main() or int main(void), its just 2 ways of accomplishing the same thing, since after all the programs both will run fine with whatever style is used?

I was taught this way without the use of int main(void) between 2000-2004 back in college.










Quite a big discussion here

http://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid
As the linked StackOverflow question/answers indicate, the difference would only really affect C.

As noted therein in one of the answers, In C, when declaring a function prototype, an empty argument list basically meant that you could pass any parameters to that function. In order to make a function and have it take no arguments at all, you would have to specify "void". C++ of course being a superset of C allows you to do this but an empty parameter list means the same thing.

Personally, any C++ main() routine I write (I don't write C/C++ very often, though) get's the full int main(int argc, char** argv) definition. (Should be noted that char** argv is identical to char* argv[] as a parameter type, though I'm not sure how I got into that habit)Quote
Personally, any C++ main() routine I write (I don't write C/C++ very often, though) get's the full int main(int argc, char** argv) definition. (Should be noted that char** argv is identical to char* argv[] as a parameter type, though I'm not sure how I got into that habit)

Being explicit with the parameter types I guess might be better practice vs leaving it undefined (). There are times when you have to be explicit or else you end up with strange problems or compiler warnings or errors. When compiling it was my understanding that the compiler whether int main() or int main(void) basicially interpreted it the same such as when using cout and printf(). However I have read that printf() seems to run faster than cout calls so if the compiler accepted both as equal when compiling there wouldnt be a speed advantage between one and the other when they are executed, so the statement that a software engineer told me years back may be completely false with how the compiler compiles the source from what we can read as source code into machine language.

Thanks for the inputs on this subject


Discussion

No Comment Found