C ++ standard forbids prototype void main ()?

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

Implementation should not predetermine the main function. This function should not be overloaded. It must have a return type of int, but otherwise its type is determined by the implementation.

I am not a native speaker of English. I'm not sure what it means, but otherwise. Should a different type of return be prohibited or given the right to a C ++ compiler?

So what is the answer?

+6
c ++ standards main
source share
6 answers

The text you specify prohibits the declaration of main to return void . This allows you to vary the arguments that are included, but not in the return type.

+12
source share

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

It must have an int return type, but otherwise its type is from the implementation.

means it might look like this:

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

etc .. and others.

+7
source share

A type contains more than just a return type. Therefore, the return type must be int, but you are free, given the remaining argument, i.e. You can, for example, choose between

 int main() 

and

 int main(int argc, char **argv) 
+2
source share

The standard says that the return type must be int , but the rest of this type is implementation-friendly. For example, you can make the standard (but not very useful) C ++ compiler that was used.

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

From Wikipedia :

In C and C ++, the prototype of the function of the main function is as follows:

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

The parameters argc, the argument count and argv, vector vector, respectively, give the number and value of the command line arguments of the program. The names argc and argv can be any valid identifier, but the use of these names is common. Other platform-specific formats are also allowed by the C and C ++ standards; for example, Unix (although not POSIX.1) and Microsoft Visual C ++ have a third argument, providing a software environment, otherwise accessible via getenv in stdlib.h:

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

Mac OS X and Darwin have a fourth parameter containing arbitrary information provided by the OS, such as the path to the binary executable:

 int main(int argc, char **argv, char **envp, char **apple) 
+1
source share

As for the parameters, this allows

  • int main ()

  • int main (int argc, char * argv [])

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

But for the standard return type, there must be an int for the purpose of matching.

0
source share

The goal is to say that aspects of the main function type, other than the return type, are determined by the implementation. This means that this declaration is permitted in this section 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 implementations are allowed to have different types of arguments.

0
source share

All Articles