Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> writes:
On 16.06.2024 22:32, Keith Thompson wrote:
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
[...] K&R at
least seems to say that 'void' can only be declared for the
return type of functions that do not return anything.
[...]
No version of C has ever permitted "void main" except when an
implementation documents and permits it. [...]
>
I cannot comment on main() being handled differently than
other C functions. I was just quoting my old copy of K&R.
First or second edition?
But main() *is* handled differently than other functions, and
that's important to understand. It's effectively called by the
environment, which means that your definition has to cooperate
with what the environment expects. What's slightly weird about
it is that it can be defined in (at least) two different ways,
with or without argc and argv.
Similarly, signal handlers and qsort comparison functions are code
that you write that's invoked by the environment or the runtime
library. You don't get to change what type they return and expect
your program to work correctly.
This is all in the "Program startup" subsection of section 5 of any
edition or draft of the C standard, and it hasn't changed much from
C89 (where it's in section 2) up to the latest post-C23 draft.
I don't understand what you mean with "no version of C has
ever permitted", given that my C compiler doesn't complain.
I mean that no edition of the C standard has ever mentioned "void
main". The only explicitly permitted return type has been int since
the first standard in 1989. In K&R1, there was no void keyword.
(I think the "void" keyword was introduced before 1989, but the
ANSI C standard formalized it.)
"void main" does not require a diagnostic, so perhaps "permitted" was
not the best word. But a conforming compiler can reject a program
that uses "void main" (that's one of the allowed consequences of
undefined behavior).
WRT return value to the environment I'd expect any random
or arbitrary value being returned in case that non had been
explicitly specified to be returned.
Feel free to expect that. The standard says that, unless the
implementation documents that "void main" is permitted, the behavior
is undefined. Not just the status, the behavior of the program.
It happens that, for most compilers, the actual behavior of "void
main" is relatively harmless -- but why take that risk?
If I want a defined exit status (which is what I usually
want) I specify 'int main (...)' and provide an explicit
return statement (or exit() call).
Why would you ever not want a defined exit status, given that it's
easier to have one than not to have one? (Since C99 an explicit
return or exit() is optional.) I can't think of any reason *at all*
to use "void main" in C with a hosted implementation. Can you?
(If you don't care about the exit status, you can just write
"int main" and not bother with a return statement or exit() call.
The exit status will be 0, but that's not a problem if you don't
care about it.)
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */