2

I have an infinite loop here, but why?

int end = 5;
for(int i = 0; i < end, printf("at condition i=%d\n",i); ++i) 
{
    printf("inside i=%d\n",i);
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Semih Kekül
  • 399
  • 3
  • 14
  • Related to [What does the comma operator , do in C?](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c) ... maybe a dup? Usually it is used in the [initialization section of the for loop](http://stackoverflow.com/a/18444099/1708801). – Shafik Yaghmour Jun 26 '14 at 12:59

6 Answers6

6

The left operand of the comma operator is evaluated as a void expression, the evaluated result of a comma operator is the result of the right operand. So the "if" part of your for loop looks at the result of the printf, which is higher than zero, meaning it will never end.

You can fix it by swapping them:

int end = 5;
for(int i = 0; printf("at condition i=%d\n",i), i < end; ++i) 
{
    printf("inside i=%d\n",i);
}

But better is to not do this at all. It is not very readable.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 1
    -1 This has nothing to do with associativity and it has everything to do with how the comma operator works, C11 6.5.17 "`The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value`" – Lundin Jun 26 '14 at 12:26
  • @Lundin thanks, I messed the terms a little, I updated my answer. – Bart Friederichs Jun 26 '14 at 12:29
4

The comma operator expression i < end, printf("at condition i=%d\n",i) is used as condition. Its value is its right operand, which is the return value of printf.

The return value of printf is the number of characters it outputs, that's never zero in this case, so it's an infinite loop.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

If compiled with -Wall,

warning: left-hand operand of comma expression has no effect

So i < end has no effect. printf() return no.of characters it printed.
Based on return value(Non zero value here), infinite loop occurs.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
2

You need to learn about comma operator. The result of comma expression is the result of right operand. In controlling expression i < end, printf("at condition i=%d\n",i), i < end is evaluated and its result is discarded.
So, the loop is controlled by the value of the expression printf("at condition i=%d\n",i) which returns number of characters that it prints and hence it evaluated always as true here and results in an infinite loop.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

Because

1) i < end, printf("at condition i=%d\n",i) returns the same value as printf("at condition i=%d\n",i).

2) printf() returns number of printed characters (it is not zero in this case)

So condition in your loop is always true.

Ilya
  • 4,583
  • 4
  • 26
  • 51
0

In the expression i < end, printf("at condition i=%d\n"), the comma operator causes the two expressions to be evaluated in sequence, but the result of the first expression is discarded, and only the result of printf is used to control the loop. Since printf will always return non-zero for that format string, the loop never terminates.

Use && instead:

for (int i = 0; i < end && printf("at condition i=%d\n"); i++)
John Bode
  • 119,563
  • 19
  • 122
  • 198