Why is return 0 optional?

后端 未结 4 1421
遥遥无期
遥遥无期 2020-11-27 21:48

Why, if I write

int main() 
{ 
    //... 
}

do I not need to write return 0; at the end of the main function? Do

4条回答
  •  攒了一身酷
    2020-11-27 22:28

    The most recent C (currently that's C99 with a few amendments) returns 0 from main by default if there is no explicit return statement at the end of the function, and control flows off the function's end (see 5.1.2.2.3 in C99 TC3). This is because most often one would write such a form of return anyway.

    In C89 you need to return something there - it has no such implicit return. But the compiler is by no means required to diagnose such a mistake (see 3.6.6.4 in the C89 draft and 6.9.1/12 in C99 TC3).

提交回复
热议问题