Possible Duplicate:
C++ Comma Operator
I had been looked if statement in C Language. like this.
if (a, b, c, d) {
blablabla..
blablabla..
}
What's the meaning of this if statement ?
Possible Duplicate:
C++ Comma Operator
I had been looked if statement in C Language. like this.
if (a, b, c, d) {
blablabla..
blablabla..
}
What's the meaning of this if statement ?
What you have there is an example of the comma operator. It evaluates all four expressions but uses d
for the if
statement.
Unless the expressions other than d
have side effects (like a++
for example) they're useless. You can see it in operation with the mini-program:
#include <stdio.h>
int main (void) {
if (1,0) printf ("1,0\n");
if (0,1) printf ("0,1\n");
return 0;
}
which outputs:
0,1
Most people use this without even realising it, as in:
for (i = 0, j = 100; i < 10; i++, j--) ...
The i = 0
, j = 100
, i++
and j++
are components of two full expressions each of which uses the comma operator.
The relevant part of the standard is C11 6.5.17 Comma operator
:
Syntax:
expression:
assignment-expression
expression , assignment-expression
Semantics:
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.
EXAMPLE:
As indicated by the syntax, the comma operator (as described in this subclause) cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers). On the other hand, it can be used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the function call:
f(a, (t=3, t+2), c)
the function has three arguments, the second of which has the value 5.
the comma operator: evaluate a and b and c and d in sequence and return the result of d.
It evaluates a, then b, then c, then d, and uses the value of d
as the condition for the if
. The other three are evaluated, but normally only for side-effects -- the results are discarded.