Hacker News new | past | comments | ask | show | jobs | submit login

Rants about void main seem archaic, unjustified, and pretentious.



How so? The argument as I understand it is that all C89 standard-compliant compilers work with "int main" (and work correctly), regardless of whether or not the target OS has exit statuses. Not all compilers work with "void main", and those that do will yield executables that always produce an exit status of zero. While there are times you might write C code that doesn't need to be portable (in which case "int main" and "void main" are effectively the same if your system doesn't use exit statuses), there is no good reason for a C textbook to teach the non-standard approach that only works in certain non-portable cases. If a textbook does so, that's likely a sign that the authors made other poor decisions.


No, compilers that accept "void main" do not necessarily produce executables that produce an exit status of 0. I just tried it with gcc on Ubuntu, and the exit status was 1.

And the validity of "void main" doesn't depend on whether the system uses exit statuses. An implementation may accept forms of main other than the ones defined by the language, including "void main()" -- but if it doesn't, the behavior of such a program is undefined.

The rules are different for freestanding implementations, which are generally for embedded systems; for such systems, the program entry point is entirely implementation-defined, and might not even be called "main". But for hosted implementations, there's no advantage at all in using "void main" vs. the correct "int main(void)".


> No, compilers that accept "void main" do not necessarily produce executables that produce an exit status of 0. I just tried it with gcc on Ubuntu, and the exit status was 1.

Interesting. I wonder why gcc does it that way.

> And the validity of "void main" doesn't depend on whether the system uses exit statuses. An implementation may accept forms of main other than the ones defined by the language, including "void main()" -- but if it doesn't, the behavior of such a program is undefined.

I don't think we're disagreeing. I'm saying that "void main" is never preferable to "int main", and the only time it's equal to "int main" is if you're writing code specific to an implementation for which "void main" is not undefined and which lacks exit statuses, making any return statement in "int main" equivalent to returning from "void main".

> But for hosted implementations, there's no advantage at all in using "void main" vs. the correct "int main(void)".

Agreed. That's why it makes no sense for textbooks to teach void main.


Agree. I've used "void main(void)" on embedded applications where 1. there is no OS under you. 2. it can never "exit"

void main(void) { init(); for(;;) { web_tick(); serial_tick(); tcp_tick(); etc. } }

An example is the horrible Dynamic C compiler from z-world (or digi as they are now called). Guys who complain about "violating standards" clearly haven't worked in diverse enough fields on broken, shitty and half-standard compilers ;)




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

Search: