Friday, November 17, 2006

Difference between main()/void main()/int main()/int main(void)/int main(int argc, char *argv[])

I thought this is helpful to novice


A very common question is "What's the difference between void main and int main?". This particular FAQ tries to answer that and more, covering other versions of the main() implementation.

The first thing to note is that this is one of those topics that people seem to like to argue over for hours, days and more. Some arguments are valid, some are not, and some are just plain old opinion.

The C and C++ standards differ when it comes to main(), so I'll detail each one separately.

For C

Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:

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

Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

The first option is used when you do not require access to the command line arguments.

The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.

The return type of main() must always be an int, this allows a return code to be passed to the invoker.

Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.

For C++

The following are acceptable uses:

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

The first option follows similar conventions to those used by C99.

The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.

Again, the return type must always be an int, and the function should return 0; at the end, but it is not required by the standard.

(C) The difference between int main() and int main(void)

A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

int foo();

In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.

What's the deal with void main()

Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

void foo(void);

A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.

Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.

But what about int main(int argc, char *argv[], char *envp[])

As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.

And finally, this page gives some more background information as to why void main is bad.

No comments: