Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this comma do in the *test* portion of a for loop?

int c = someIntegerValue;

// Some code...

int i;
for ( i = 0; i < 5, i < c; i++ ) {
...
}

My compiler says error: expression has no effect, which sounds about right. So, which of those 2 comparisons will be used here? My guess is the i < c is ignored, but I wanted some confirmation from others as I am not in a position to run this code yet...

like image 424
Michael Dorgan Avatar asked Jun 10 '13 20:06

Michael Dorgan


People also ask

What does a comma in a for loop do?

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.

What is the purpose of comma operator?

A comma operator is used as a separator for multiple expressions at a place that requires a single expression. When a comma operator is placed in an expression, it executes each expression and returns the rightmost expression. In the above syntax, multiple expressions are separated using a comma operator.

Can we use comma in for loop in C?

The commas you see in C for loops are not part of the syntax of the for loop specifically. They are just manifestations of the comma operator. Commas are major separators between arguments in function calls and between parameters in function declarations, but semicolons are not used.

What is the comma operator in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.


6 Answers

The statement

i < 5, i < c

Uses the comma operator, which evaluates all the expressions from left to right but only produces the value of the rightmost one. This means that the expression i < 5 is evaluated and discarded, while the expression i < c is evaluated and actually used by the loop.

I assume that the author meant to write something like this:

i < 5 && i < c

which actually considers both of the expressions.

That said, I'm not sure why this is a compiler error and not a compiler warning. This is legal code, though it's almost certainly a bug. Perhaps you have set the compiler to report errors on warnings?

Hope this helps!

like image 83
templatetypedef Avatar answered Nov 04 '22 14:11

templatetypedef


That's a comma operator. It evaluates its left and right operands (always in that order), and yields the result of the right operand. It makes sense to use it if the left operand is evaluated for its side effects; since i < 5 has no side effects, your compiler warns you about it.

It's i < 5, not i < c that's ignored. The code is equivalent to:

for ( i = 0; i < c; i++ ) {
     ...
}

It's difficult to guess what it was intended to be. Perhaps the author actually meant:

for ( i = 0; i < 5 && i < c; i++ ) {
    ...
}
like image 27
Keith Thompson Avatar answered Nov 04 '22 15:11

Keith Thompson


It is i < 5 that is ignored.

The complete expression is:

i < 5, i < c

Which is two separate expressions joined by the comma-operator.

The comma-operator works by evaluating the first expression (i < 5), then the second expression (i < c), and the full expression takes on the value of the second part.

The first part of the expression only matters if it has side-effects.

It is commonly, and correctly used this way:
example

for(i = 0, j = 10; i < 10; ++i, --j)
/* i goes from 0-10, while j goes from 10-0 at the same time */

But in the way shown in your code, it has no meaningful purpose, and only confuses other people.

like image 36
abelenky Avatar answered Nov 04 '22 15:11

abelenky


The result of the comma operator is the value of the right operand.

Here the left operand (i < 5) has no side-effect so

i < 5, i < c

is actually equivalent to

i < c
like image 27
ouah Avatar answered Nov 04 '22 14:11

ouah


It's a result of using the comma operator. The left expression is evaluated, and then the result is discarded. After that the right expression is evaluated. The comma operator's result is the result of the right expression.

Since the left expression (i < 5) is a no-op, you get the warning you're seeing. The question now is "what did the author intend?" Without more context, we can't really say, but it's likely one of these:

  i < 5
  i < c
  i < 5 && i < c
like image 26
Carl Norum Avatar answered Nov 04 '22 14:11

Carl Norum


Here , comma operator plays a role. Means, it evaulates from left to right and returns the last expression. So i<5 is evaluated, i<c is evaluated but returns i<c result as its the last expression.

i < 5, i < c is completely pointless. It has not difference with i < c.

Depending on authors logic its a mistake.

like image 34
Shiplu Mokaddim Avatar answered Nov 04 '22 16:11

Shiplu Mokaddim