1. ## something weird

hello today i was trying something with linked list to initialize a var in the 2nd field of the for loop using the , operator but i got into a forever loop i want to know if this is defined behaviour or not ? since 1st field only does stuff once and the 3rd field does it after loop runs
Code:
#include <stdio.h>
int main(void)
{
int x=1;
int b;
for(; x<10  , b=x  ;x++ )
printf("%d\n",x);
getchar();
return 0;
}

2. you are setting b equal to x, I believe that is an assignment not a comparison. It should always be true correct?

3. It's not an infinite loop, it will eventually break when b == 0. You loop basically says:

for <initialize nothing> ; so long as (discarded: x<10) b=x is non-zero; increment x

Quzah.

4. The results are well defined: look up the comma operator.

The expression "p, q" evaluates p, then evaluates q, and yields a result of q. Similarly x < 10, b = x evaluates x < 10 first, then assigns b to be x, and yields the value of b. b (like x) is always non-zero - or will be non-zero for quite a few iterations - so the loop never terminates.

Try changing the loop to
Code:
for(; c = (x<10  , b=x)  ;x++ )
This is exactly the same, except it catches the value of the expression "x < 10, b = x" in the variable c. If you watch the value of c, you will find that it holds the value of x each time through the loop.

5. What he really should be doing is this:
Code:
for( ; b = x && x < 10; x++ )
Otherwise, it's still not going to break when x is bigger than 9.

Quzah.

6. Originally Posted by quzah
What he really should be doing is this:
Code:
for( ; b = x && x < 10; x++ )
Otherwise, it's still not going to break when x is bigger than 9.

Quzah.
i think that wont work since it will evalute to false and wont run the loop i tried it and it didn't run the loop

7. b = x will only evaluate as false if x is 0.

EDIT: Well, that's true I suppose, but not the point. = has lower precedence than && so, x && x < 10 will be evaluated and assigned to b instead. Sorry.

8. oke here i got it i think when i puted braces around b=x i think it doesnt makes it get discarded and gets initialized to x then after that test is made
Code:
#include <stdio.h>
int main(void)
{
int x;
int b;
for(x=0; (b=x) , x<10 ;x++)
printf("%d\n",b);
getchar();
return 0;
}

9. ...And now you're back to where you started. Here's an idea: how about not using obscure loop control constructs?

Code:
for(x = 0; x < 10; x++)
{
b = x;
printf("%d\n",b);
}
Easy.

10. Originally Posted by Sebastiani
...And now you're back to where you started. Here's an idea: how about not using obscure loop control constructs?

Code:
for(x = 0; x < 10; x++)
{
b = x;
printf("%d\n",b);
}
Easy.
yah i know thats easy :P but i was just curious abt initializing in the middle when i added (b=x) in braces it made it work ..

11. It wasn't the braces that made it work, it was putting it before the comma.

12. yes your right it makes sense aswell

13. Although the three expressions of a for-clause can be anything, it is normally considered proper for the first and third expressions to be manipulators of state, and the second expression to be an idempotent boolean expression. In other words, changing state inside the conditional expression is really, really bad form.

This code isn't really clever, it's just confusing (obviously confusing enough for you to code up this bug). I've seen some rather complicated expressions in for-clauses but nothing like that before. Don't do it.