Both constructs are equivalent in behavior.
Regarding preferences:
The C Programming Language, Kernighan & Ritchie,
uses the form
for (;;)
for an infinite loop.
The Practice of Programming, Kernighan & Pike,
also prefers
for (;;)
"For a infinite loop, we prefer for(;;) but while(1) is also popular. Don't use anything other than these forms."
PC-Lint
also prefers
for (;;)
:
716 while(1) ... -- A construct of the form while(1) ... was found. Whereas this represents a constant in a context expecting a Boolean, it may reflect a programming
policy whereby infinite loops are prefixed with this construct. Hence it is given a separate number and has been placed in the informational category. The more conventional form of infinite loop prefix is for(;;)
One of the rationale (maybe not the most important, I don't know) that historically for (;;)
was preferred over while (1)
is that some old compilers would generate a test for the while (1)
construct.
Another reasons are: for(;;)
is the shortest form (in number of characters) and also for(;;)
does not contain any magic number (as pointed out by @KerrekSB in OP question comments).
for (;;)
because it's told to be "more idiomatic" for some reason, whereas I preferwhile (1)
because it's more readable. – Edettewhile (1)
for it's simplicity, and for that reason it's also more readable as @H2CO3 says. – Cataractif not, why do people use it
covers the general query of this question ;-) – Decimalizewhile(1){...}
because it is shorter. Thefor(;;) {...}
variant has some implied semantics, which is unnecessary. (BTW: for SQL, I prefer... WHERE 1=1
, which is also very explicit) – Audiogenic