5 ms·
Not true. The kernel & shell makes the values available on the stack and the runtime can choose to link to them or not. The ABI also details how and where the
by oso2k 6y ago
Not true. The kernel & shell makes the values available on the stack and the runtime can choose to link to them or not. The ABI also details how and where the values should be placed.
- chrisseaton 6y ago> Not true. Yes true. Check 5.1.2.2.1 and 5.1.2.2.3 in the standard. If you don't treat 'main' specially you won't have a conforming C compiler. It's unique in accepting different signatures, and for its return value syntax and semantics. You cannot just compile it as a normal function and link it with a runtime routine. You need compiler support for it.
- oso2k 6y agoThis is the C Standard specifying its VM to match the assumptions of a *nix environment. `main` isn't so special, IMO. Conformance to the C Standard is always optional, see C++, golang, Rust. However, conformance to ABI is less optional.
- kps 6y ago> You need compiler support for it. There are probably implementations that require compiler support, but it is not required by the standard and not true of many common implementations. Support is by the caller, as described in others' parallel comments. > You cannot just compile it as a normal function and link it with a runtime routine. You can. As a concrete example, for the following two programs, int maim(int argc, char *argv[]) { return 0; } int main(int argc, char *argv[]) { return 0; } the assembly code produced by gcc 9.3 x64 is identical except for the spelling of the identifier.
- chrisseaton 6y agoNow try int maim(int argc, char *argv[]) { } int main(int argc, char *argv[]) { } They compile to different code. Can you figure out why? A function called main is treated differently in this regard to other functions, which is enforced by the standard, and so has to be implemented by the compiler.
- kps 6y agoDamn, you're right: Currently, “reaching the } that terminates the main function returns a value of 0”. In my defense, that was different in C89, which was the last one I worked on implementing and knew completely: “If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined” and “Reaching the } that terminates a function is equivalent to executing a return statement without an expression.”
- saagarjha 6y agoA fun fact related to the other points: you can legally call main in C, but not in C++ (it will usually work, though). Another fun fact is that on some platforms (Linux, macOS) you can "dynamically load" a (dynamically linked) program by calling dlopen on it and using dlsym to grab the address of main and just calling it :)
- kps 6y ago> you can legally call main in C Good point. That makes it relatively difficult (not impossible) to compile `main` differently from other functions, other than diagnosing bad signatures if that's what the hosted environment requires, since it has to be callable like other functions.
- wruza 6y agoTechnically, local C ABI may be callee-cleanup, and then main() must be special in that it's neither variadic nor fixed-prototype. But in a major compatible reality it's always cdecl, thus not much different from foo(void) being called with foo(int, ptr, ptr) via a prototype mismatch which a compiler is surely unable to detect (that's UB for that exact non-existent ABI reason, as it is for almost half of C). It is only special for a return statement really.