No loop condition in for and while loop

c, loops

Solution

The standard requires that the omitted condition for `for` loop is replaced by a non-zero constant:

From C11 6.8.5.3: (emphasis mine)

6.8.5.3 The for statement

1 The statement for ( clause-1 ; expression-2 ; expression-3 ) statement behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.134)

2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

Since there's no such requirement for `while` loop (if the condition is omitted), I believe, it's left to the implementation of compiler.

Problem

``` while(cond) // fine for(;cond;) //fine ``` but when I remove the conditional part ``` while() //syntax compilation error for(;;) //Infinite loop ``` How these loops are internally implemented ? Or,how does compiler (parser) know that empty condition in `while` is error and in `for` as Infinite? I didn't find anything about this particularly, I think guys like me (who are beginner) in C might have same confusion

Original source