Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?

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.



Starting 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.


> 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...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: