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

(C) main

The function main() is the entry point of your program.

There are two syntaxes:

int main(void) { /* 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.


You can leave out the arguments of main().

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


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

int main() [1-4]

'main' links

Code links

References

  • 1) 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.
  • 2) 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.
  • 3) http://c-faq.com/ansi/maindecl.html


last edited (December 22, 2006) by bilderbikkel, Number of views: 1195, Current Rev: 2 (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.