2 ms·
One of the commentators mentions some... frustration... with void main() Out of curiosity, is there any reason to have a return value from main() in something
by sliverstorm 16y ago
One of the commentators mentions some... frustration... with void main()
Out of curiosity, is there any reason to have a return value from main() in something like a microcontroller, where there's nobody and nothing (that I know of) to care what main() returns?
- btmorex 16y agoIf you're running on a microcontroller, with no OS underneath you don't even need to use main(). You of course need some entry point for your loader to start executing, but you can pick whatever convention you want.
- Jach 16y agoThe only reason to have a return value for main() is if you yourself care, and sometimes you might. For my real time operating system class I have a Startup.s file that ends up calling the OS's main(), and while I don't care about its return value I might want to flash certain LEDs if it returned something other than 0. If I had to ship it I also might want to make it log something to some persistent memory (like an SD card) so I could debug a customer's problem without being there with a debugger under the same circumstances. Also I'm working on getting dynamic applications on the system, and the OS might be interested in their return values.
- cperciva 16y agoOut of curiosity, is there any reason to have a return value from main() in something like a microcontroller, where there's nobody and nothing (that I know of) to care what main() returns? It's very unlikely... but yes, just possibly. There are some very wacky calling conventions out there; among them, "caller reserves space for result on the stack after the function arguments". If you run into this particular variety of crazy, declaring main as returning void will result in the compiler looking in the wrong place for main's parameters, with obvious and very rapid breakage resulting. I'd say that you don't need to worry about this, except that microcontrollers are exactly the sort of niche environment where you're likely to encounter craziness -- so it's better to play it safe and declare main correctly.
- JoachimSchipper 16y agoStarting the program is almost entirely implementation-defined in freestanding environments (i.e. those without a host OS), so I don't think your example works. Of course, there are reasons not to use "void main" - for one, a strict C99 compiler will not compile it.
- jjs 16y ago> There are some very wacky calling conventions out there; among them, "caller reserves space for result on the stack after the function arguments". Well, that's not very tail-call friendly...