Your question isn't really about bool
(which modern C does have if you #include <stdbool.h>
), it's about the best way to write an infinite loop.
Common idioms are:
while (1) {
/* ... */
}
and
for (;;) {
/* ... */
}
The latter looks a little obscure, but it's well-defined. Any of the three expressions in a for
loop header can be omitted; if the second expression, which controls when the loop continues to execute, is omitted, it defaults to true.
while (1)
is probably the most straightforward method -- but some some compilers might warn about a condition that's always true. for (;;)
likely avoids that, since there is no (explicit) expression to warn about.
I personally wouldn't use a do
/while
loop, because the condition is at the bottom.
There are trickier ways to write an infinite loop (while (1 + 1 == 2)
et al, but none of them are really worth the effort.
Either while (1)
or for (;;)
will be perfectly clear to anyone with a good understanding of C.