Value in using: int main(int argc, char* argv[])

Hi,

I was wondering what the value is of writing the main function as:

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

as oppposed to: int main()

I see the first example used quite often in programs and don't really understand the value or the difference. I never see the arguments ever used in the programs either. Can anyone enlighten me on the subject?

Also, is there a reason I might want to use void main() rather than int main()? I see that often as well.

Thanks!
Return 0;
Last edited on
1.) This allows you to handle the command line arguments..., argc is the count of how many arugments there are, and argv is an array of the arguments (as char*s)

2.) void main()? It's not really standard practice...so you shouldn't really do it.
void main() is evil. Please don't use it.

(It is a non-portable compiler kludge extension that some anti-genious invented in pre-history and which has plagued C programmers ever since.)

The reason it exists is still unclear. The reason it continues to exist is because too many people (including authors of C and C++ books, and some suit-managed compiler writers) just don't know it is the Wrong Thing.

Some further reading (to help obtain enlightenment):
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
http://www.research.att.com/~bs/bs_faq2.html#void-main
http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143
http://users.aber.ac.uk/auj/voidmain.shtml

In short, remember that main() is not a normal function! That's right, it is a special, abnormal function. It should only be called by the OS (from your POV) and should always return int. Anything else is non-standard and likely to cause problems with the vast majority of compilers (even if it seems to work just fine with your compiler).

Funny, no?
Last edited on
Great info firedraco and Duoas, thanks for your help.

Return 0;
Topic archived. No new replies allowed.