tags:

views:

283

answers:

6

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 whats the answer?

thanks.

+8  A: 

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.

anon
+9  A: 

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.

bmargulies
This is a favorite item to catch for participants in the newsgroups. However, can one cast the return type of `main` to `void`?
Thomas Matthews
Sorry, I don't follow your question. Where would you be in a position to be receiving the return type of main?
bmargulies
@bmargulies: in another C program that took the first program's result as input? Still, weird question.
Adriano Varoli Piazza
@bmargulies, `main` could be a recursive function, or could be called by some other part of the program.
Iceman
@Kevin are you sure main() can be recursive in standard C / C++? Also, where else in a program could you call main from in C / C++? This is not Java.
Adriano Varoli Piazza
@Adriano, good catch, +1 to you. Recursive main is legal C but specifically illegal C++ (although I've used compilers that allowed it), and this is a C++ question.
Iceman
+2  A: 

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)
Frank
or int main( int argc, char ** argv, char** env) on linux
caspin
not required by C++ standard
anon
But probably a requirement of the compiler.
Martin York
The C++ standard doesn't forbid additional arguments (although the two signatures you quote must be allowed), so Caspin is correct on some compilers. It does forbid `void main()`.
David Thornley
A: 

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.

Ashish
Number 3 is not required by the C++ Standard.
anon
The third is allowed but not required by the C++ Standard, along with infinitely many other possible signatures. The first and second must be allowed by a conforming compiler.
David Thornley
Would it be fair to say that the standard allows main to take any set of parameters beyond the first two, but accessing any further parameters will either be implementation-defined behavior (if the implementation defines them) or Undefined Behavior (if it does not?)
supercat
A: 

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.

Omnifarious
+1  A: 

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)
Iceman