Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
The main function is the entry point of your program. The C++ Standard [1] 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?
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:
/* Correct C syntax, not correct C++ syntax */ |
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 void for more detailed discussion and references.
Correct C++ syntax of a main() that does not use its arguments, is [1]:
//Correct C++ syntax |
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!!! |
Below is an example showing all parameters a user entered.
#include <iostream> |
This means if you start the program with e.g.
testMain Hello World |
Your output will be something like:
0 : testMain |
main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only |
The definition |
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. |
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.