Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 04. Jun 2024, 00:23:57
Autres entêtes
Organisation : None to speak of
Message-ID : <87le3l1ugi.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
bart <
bc@freeuk.com> writes:
[...]
At this point someone will suggest a macro this:
>
#define forever for(;;)
When I was first learning C, I defined a macro, something like:
#define ever ;;
so that I could write
for (ever) {
/* ... */
}
At the time, I thought it was very clever.
I still think it was very clever. But I no longer think that's a
good thing.
All that suggest sto me is that the language *needs* an explicit
endless loop!
No, it doesn't.
There are multiple valid and idiomatic ways to write an infinite
loop in C:
for (;;)
while (1)
while (true) // requires C99 or later and #include <stdbool.h>,
// or C23 or later without the #include,
// or your own "true" macro.
There is nothing wrong with any of them. All C programmers should
immediately recognize each of them as an infinite loop. The compiler
might have to do a few different things internally to process each
one -- and that makes no difference to me as a programmer. If a
compiler generated different code for different forms, I probably
wouldn't notice. If I bothered to check, I'd be mildly curious
about the reasons, and annoyed if one form was more efficient.
A language designed from the beginning with syntactic and semantic
elegance in mind might have only one explicit form of infinite loop
(though something like "while (true)" or "while (1+1==2)" would
still be allowed). C is not that language.
I suspect some of the people in this thread saying that one form
is obviously better than the others are joking.
It doesn't matter.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */