views:

133

answers:

2

Insomuch as I understand "for(;;)" has no initial condition, no test condition and no increment condition, and therefore loops forever, I am curious why the test condition succeeds each loop.

Does the empty expression ";" evaluate as true in C? Or is this a special case handled by compilers?

A similar, but unrelated question.

+13  A: 

This is by the definition of the for statement in the C language. 6.8.5.3/2 "The for statement":

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

Michael Burr
Perfect. Thanks Michael! :D
schultkl
Here is a link to the current draft C99 specification: http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf . The for-loop specification is on page 136.
schultkl
+5  A: 

C language has no such thing as "empty expression". If any expression is missing from the for statement, syntactically it means that the entire expression is omitted, not that it is there, but empty.

A for loop with an omitted second expression loops forever because the specification of for statement says so. I.e. it is a dedicated feature specific to for alone, not something more generic.

Additionaly (a terminological nitpick) only the second expression is really a condition. The first and the third are not really "conditions".

AndreyT
Thanks for the clarifications AndreyT! :)
schultkl