[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppMain

(C++) main

The function main is the entry point of your program. The C++ Standard (item 3.6.1.2) states that it has one of the following syntaxes:

int main() { /* Your code */ }


and

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


What does this all mean?
int main means that main is a function returning an integer.
(int argc, char* argv[]) means that the function takes two arguments:
int argc: argc is an abbreviation of 'argument count'. It is a (positive) integer. This give you the number of parameters the user entered when starting the program, which equals 1 if he didn't enter parameters.
char* argv[] is an array of pointers to type char (say: 'character pointer', see also char*). In here, all the arguments are stored that the user entered when starting the program. At index 0, the program's name is stored.


When main's closing bracket is reached, the effect is equivalent to (Standard, 3.6.1.5):
return 0;


You could leave out the arguments of main. Then the correct syntax depends on whether you program in C or C++.

Correct C syntax is:

int main(void)
{
  //Your C code
  return 0;
}


Unlike C, where one writes void when a function does not have arguments, in C++, when a function has no arguments, nothing is written between the brackets. See the keyword void for more detailed discussion and references.

Correct C++ syntax of a main() that does not use its arguments, is [1]:

int main()
{
  //Your C++ code
}


Note that the standard states that the closing bracket of main() must have the same effect of returning zero [6]. Therefore, return zero can be omitted, but many people like to keep it in.

Incorrect/non-standard is [1-5] (although with some compilers it might compile):
void main() //INCORRECT!!!
{
  //Your code
}


Below is an example showing all parameters a user entered.
  1. include <iostream>
int main(int argc, char* argv[]) { for(int i=0; i!=argc; ++i) { std::cout << i << " : " << argv[i] << std::endl; } }
This means if you start the program with e.g.
testMain Hello World
Your output will be something like:
0 : testMain
1 : Hello
2 : World


Should I use void main() or int main()?

int main() [1-5]

'main' links

Code links

References

  • 1) C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 3.6.1.2
  • 2) From http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3 : main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int. Some compilers accept void main(), but that is non-standard and shouldn't be used. Instead use int main().
  • 3) Herb Sutter. Exceptional C++. ISBN: 0-201-61562-2. Item 21: void main() is nonstandard and nonportable.
  • 4) From Bjarne Stroustrup's homepage (http://www.research.att.com/~bs/bs_faq2.html#void-main) :
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it even been C.
  • 5) From the The alt.comp.lang.learn.c-c++ FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html#q3.4: 3.4 Why does everyone make so much fuss about "void main()"?. Because the return type of the main() function must be int in both C and C++. Anything else is undefined. Bottom line - don't try to start a thread about this in alt.comp.lang.learn.c-c++ as it has already been discussed many, many times and generates more flamage than any other topic.
  • 6) C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 3.6.1.5.


last edited (December 31, 2006) by bilderbikkel, Number of views: 10041, Current Rev: 32 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.