Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> writes:
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?
K&R1, the first edition, was published in 1978. K&R2, based on the
then-new ANSI C standard, was published in 1988. The 1977 date is
odd, but you clearly have a translation of the first edition.
That book is of great historical interest, but it's *not* a good
source of information of C as it is now. I don't even know where
to find a C compiler for modern systems that supports that version
of the language. Even the second edition is out of date, but it's
a good start for learning modern C.
[...]
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.
You've mentioned several things you have no idea about. Are you
interested in learning?
[...]
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.)
We're not agreeing unless you've changed your mind.
You implied that there are cases where you don't want a defined exit
status. For me, there are no such cases. I would have go out of my way
to avoid having a defined exit status, and if I used your method then
the behavior of my program, not just its exit status, would be
undefined.
(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.
Again, that's not what "void main" means. Unless the implementation
documents that it permits "void main" (Microsoft's C compiler is the
only one I know of that does so, and it's vague about the semantics),
"void main" makes exactly as much sense as "float main".
You can write "int main(void)" and omit the return statement.
You can write "int main(void)" and add "return 0;". You can write
"int main(void)" and add "return rand();" if you really want to.
[...]
Whatever current C standards - and I'm not sure what ancient
'cc' is on my system and to what standard it complies - say,
Perhaps you should find out what your ancient "cc" does. What OS
are you on? Does "cc --version", "cc -V", or "man cc" give you
any meaningful information?
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.
That's fine. A return statement or exit() call is unnecessary
in main() due to a special-case rule that was added in 1999 for
compatibility with C++. I don't particularly like that rule myself.
I choose to omit the return statement in small programs, but if
you want to add the "return 0;", I have absolutely no objection.
(I used to do that myself.) It even makes your code more portable
to old compilers that support C90. (tcc claims to support C99,
but it has a bug in this area.)
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */