K
Keith Thompson
Jens Gustedt said:Am 03.10.2012 23:07, schrieb Keith Thompson:
No it isn't. The standard has it even as example in two places. As
long as it is part of a definition and as long as you are not calling
main recursively, this is ok.
I can't find an example of "void main" in the C standard (I've checked
several versions). Perhaps my PDF reader's search function isn't good
enough. Can you be more specific?
I acknowledged that the statement isn't *quite* accurate; I stand by my
statement that it's close enough. I didn't want to go into the details
that we've discussed here at length in the past.
Take a look at section 5.1.2.2.1, and note in particular the use of the
word "shall".
A hosted implementation must accept "int main(void)" and "int main(int
argc, char *argv[])" or equivalent. It *may* (but need not) accept
other implementation-defined forms. The term "implementation-defined"
requires such forms to be documented.
If I write "void main(void) { /* ... */ }" and my implementation doesn't
explicitly state in its documentation that it accepts that form, then my
program's behavior is undefined. The implementation can accept it, or
it can can warn about it or reject it outright, or the program can
behave in any arbitrary manner at run time. If it does accept it, then
the behavior is implementation-defined -- except that if execution
reaches the closing "}", the termination status is unspecified. (I
hadn't noticed that last bit before; I'm a little surprised that it's
not implementation-defined rather than unspecified.)
Generally this "void main()" business is not a good example for things
that people should primarily learn about C. There are much more
important things to come first.
I agree that it's not all that important by itself, but I think it does
betray a certain lax attitude. For example, some of Schildt's books (at
least his older ones) use "void main()". If that were the only error,
it wouldn't be a huge deal, but it's a symptom of general sloppiness.