I know that in C compilers the main() function is called by the _start() function which has code something like this:
exit(main()); // return value of main is returned
How does _start() work when main() does not return int, for example if its return type is void, float, or something else?
If
maindoesn’t returnint, then you have an ill-formed program and behavior is undefined. Anything can happen. Your program might crash, or it might run as though nothing were wrong at all.Let’s suppose
mainreturned something other thanint, and your compiler and linker allowed the program to be made. The caller doesn’t know that, though. If the caller expects returnedintvalues to be returned in the EAX (Intel) register, then that’s what it will read to determine the return value ofmain. If your faultymainstored afloatvalue there, then it will be interpreted as anintinstead. (That doesn’t mean it will get truncated. It means the bits making up the layout of a floating-point value will instead make up anintinstead.) If your faultymainreturnedvoid, then it didn’t store anything in the expected register, so the caller will get whatever value was previously stored in that register instead.If your
mainreturns some type that it expects to store someplace that the caller didn’t’ reserve memory for (such as a large struct), then it will end up overwriting something else, perhaps something important to the clean shutdown of the program, causing your program to crash.