What happens if main() does not return an int value?

前端 未结 8 1638
渐次进展
渐次进展 2020-12-03 03:19

I know that in C compilers the main() function is called by the _start() function which has code something like this:

exit(main());         


        
相关标签:
8条回答
  • 2020-12-03 03:58

    The function will return an implementation-defined value. For example, in C++, main implicitly returns 0. In this case of a void main then this would simply be returned by _start. However, there are virtually no implementations that would allow any arbitrary return type- it's baked into the operating system that a process exits with an integral value.

    0 讨论(0)
  • 2020-12-03 04:01

    Standard implementations of C expect main to return int just because it is defined that way in the C standard. Returning something other than int (or a type compatible with int) usually results in undefined behaviour—meaning there is no way to tell what will happen.

    However, there are non-standard implementations of C, for example, the Plan 9 operating system uses void main(), here is a list their utilities' source code. Plan 9 C code is quite a bit different to K&R, ANSI, C99 or C11. Here's a link explaining how Plan 9 uses the C language.

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