Why does a main function without a return statement return value 12?

后端 未结 4 1283
北恋
北恋 2020-12-03 07:42

I have written a program that prints a table. I have not included the return syntax in the main function, but still whenever I type echo $? it displays 12.

My source

4条回答
  •  一整个雨季
    2020-12-03 08:45

    If you're at all familiar with assembly language, you may recall that the "return value" of a function is passed through the EAX register.

    In this case, the return value is being read from EAX. Which, in this case, happens to be 12.

    However, you're not explicitly setting this value, it's simply an artifact from the rest of the code (or perhaps just chance).

    As has been said, this is definitely undefined behavior. If you are simply curious why this results, please consider this explanation.

    But do not, under any circumstances, attempt to intentionally use this value as anything meaningful.

提交回复
热议问题