Why do some people use for( ; ; )
in c instead of just using while(1
)? Is there some sort of speed advantage? I cannot for the life of me find a reason to do it other than to make the code harder to read
Asked
Active
Viewed 127 times
0

Andrew Wright
- 15
- 4
-
2It doesn't require any magic literals, which is nice. – Kerrek SB May 26 '16 at 21:50
-
It depends on personal style, no speed advantage... – Edgar Rokjān May 26 '16 at 21:50
-
My guess is that they use the `for( ; ; )` to be able to put in expressions later if they want the loop modified. AFAIK there is no performance difference. – Niklas Rosencrantz May 26 '16 at 21:50
-
I think advocating while(1) for clarity is strange, when you could write while(true) instead. – Eiko May 26 '16 at 21:51
-
There is no functional difference. I do not have access to a compiler at the moment to prove it, but I would bet they produce identical assembly. – bodangly May 26 '16 at 21:51
-
1Idiom my friend, [Idiom](http://www.labri.fr/perso/strandh/Teaching/MTP/Common/Strandh-Tutorial/idioms.html) – dawg May 26 '16 at 21:53
-
1@Eiko: `true` didn't exist in C prior to C99. In C99 and later, it exists only if you've added `#include
`. – Keith Thompson May 26 '16 at 22:22 -
2Why do some people use `while(1)` in C instead of just using `for(;;)`? – Keith Thompson May 26 '16 at 22:22