What does “for(;;)” mean?

后端 未结 5 1669
甜味超标
甜味超标 2020-12-01 09:36

In C/C++, what does the following mean?

for(;;){
    ...
}
5条回答
  •  广开言路
    2020-12-01 10:06

    Even if this answer suggests that both constructs are equivalent, there's a subtle difference between both for(;;) and while(1) (which both create infinite loops) in the C language (and possibly compiler-dependent).

    Some compilers (Windriver DIABData for instance) complain about "condition is always true" when using while(1).

    Changing to for(;;) allows to get rid of the warning, probably because the latter expression is semantically stronger to create an infinite loop on purpose, and there is no "always true" condition at all (plus it's shorter to write).

    On the other hand, the C++ language doesn't make a difference about both constructs as Adrian stated in comments:

    The C++ standard states that a missing condition makes the implied while clause equivalent to while(true) and for ( for-init-statement condition opt ; expression opt ) statement is equivalent to { for-init-statement while ( condition ) { statement expression ; } }

提交回复
热议问题