does c++ standard prohibit the void main() prototype?

前端 未结 6 1790
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 18:27

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 ov

相关标签:
6条回答
  • 2020-12-11 18:37

    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.

    0 讨论(0)
  • 2020-12-11 18:41

    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.

    0 讨论(0)
  • 2020-12-11 18:46

    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.

    0 讨论(0)
  • 2020-12-11 18:48

    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)
    
    0 讨论(0)
  • 2020-12-11 18:49

    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)
    
    0 讨论(0)
  • 2020-12-11 19:00

    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.

    0 讨论(0)
提交回复
热议问题