Must the int main() function return a value in all compilers?

This question already has an answer here:

  • What should main() return in C and C++? 19 answers

  • 在C ++中,在C99和C11中,如果控制流到达main函数的末尾,则语言的特殊规则是,函数impliclty返回0


    In C++ and C99/C11, without a return statement in main function, it's default to return 0 ;

    § 3.6.1 Main function

    A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

    also read wiki page C/C++ main function

    In case a return value is not defined by the programmer, an implicit return 0; at the end of the main() function is inserted by the compiler; this behavior is required by the C++ standard.


    main must return an int , some compilers, including Turbo C++, may allow other return values, notably void main , but it's wrong, never use that.

    However in C++, if you don't explicitly return a value in main , it's the same as return 0;

    C++11 §3.6.1 Main function section 5

    A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

    return 0;
    

    Note that for C, this is only supported in C99 and later, but not supported by C89.

    链接地址: http://www.djcxy.com/p/28732.html

    上一篇: 为什么C和c ++中的主要函数的类型留给用户去定义?

    下一篇: int main()函数是否必须在所有编译器中返回一个值?