Sujet : Re: Whaddaya think?
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 17. Jun 2024, 08:16:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4onrg$hmc2$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 17.06.2024 08:20, Keith Thompson wrote:
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?
It's a translation of a "(c) 1977 Prentice Hall" original, with
no further edition mentioned, so it's probably the 1st edition?
But main() *is* handled differently than other functions,
Be assured, I don't object! It was just not mentioned that it's
a special case.
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.
I'm not sure whether my K&R copy addresses that at all. A quick
view and I see only one instance where "main()" is mentioned at
the beginning: main() { printf("hello, world\n"); }
No types here, and no environment aspects mentioned.
Also mind that (other) languages don't need to interact with the
environment. Just recently I noticed that the Algol 68 Genie
does not pass the value of the outmost block to the environment.
Environment questions, interaction with the OS, may not be part
of the language. (I'm not saying anything about the C standard
[that I don't know]. Just a comment in principal.)
What's slightly weird about
it is that it can be defined in (at least) two different ways,
with or without argc and argv.
Frankly, I have no idea about the details of evolution of the
C language. The old K&R source I have I had considered a pain;
it provoked more questions than giving answers. And since then
C changed a lot. That's why I stay mostly conservative with C
and if in doubt check things undogmatic just with my compiler.
[...]
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?
Aren't we agreeing here? (The only difference is that you are
formulating in a negated form where I positively said the same.)
(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?
Well, to indicate that there's no status information or that
it's irrelevant. E.g. as was the case in the test fragment I
posted.
In programs I typically write there's a lot things that can
possibly go wrong - and that the program cannot fix -, mostly
(but not exclusively) externalities. So it's typical that I
interrogate return status of functions, map them to a defined
set of return codes, create own codes for data inconsistencies
etc. And this status is relevant and of course returned to the
environment (often accompanied by some textual information on
stderr).
(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.)
Whatever current C standards - and I'm not sure what ancient
'cc' is on my system and to what standard it complies - say,
if I specify an 'int' return type I also want a 'return' (or
exit()) - consider it as "code hygienics" - even if it's not
necessary according to more recent standards.
Janis