Question

In section 3.6.1.2 of both C++ Standard 1998 and 2003 editions,

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

I am not a native English speaker.I do not sure what does"but otherwise" means.Whether it is to prohibit the other return type,or to give the right to C++ compiler writer?

So what's the answer?

Was it helpful?

Solution

The english you quote does prohibit declaring main to return void. It is allowing variation in the arguments that come in, but not in the return type.

OTHER TIPS

Aaargh! Yes it does. The only return type allowed by the standard is int. To quote from section 3.6.1:

It shall have a return type of type int, but otherwise its type is implementation-defined.

meaning it could look like this:

int main( float f );
int main( int x, int y );

etc. etc.

The type contains more than just the return type. Hence, the return type must be int, but you are free considering the remaining argument, i.e. you may e.g., select between

int main()

and

int main(int argc, char **argv)

The standard is saying that the return type must be int, but that the rest of the type is up to the implementation. For example, you could make a standard-compliant (but not terribly useful) C++ compiler that used.

int main(int secondsSinceSystemStart, int myFavoriteNumber, char* aFunnyJoke)

From Wikipedia:

In C and C++, the function prototype of the main function looks like one of the following:

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

The parameters argc, argument count, and argv, argument vector, respectively give the number and value of the program's command-line arguments. The names of argc and argv may be any valid identifier, but it is common convention to use these names. Other platform-dependent formats are also allowed by the C and C++ standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:

int main(int argc, char **argv, char **envp)

Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:

int main(int argc, char **argv, char **envp, char **apple)

As far as parameters are concern ,it allows

  1. int main()

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

  3. int main(int argc , char * argv[] , char * envr[])

But as per standard return type should be int for consistency purpose.

The intent is to say that aspects of the type of the main function other than the return type are implementation defined. That means this declaration is allowed by this clause of the standard:

int main(int fred, char *bouncy);

but not this one:

void main(int fred, char *bouncy);

Its return type must be int, but the implementation is allowed to have different argument types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top