简体   繁体   中英

What's the meaning of if(a,b,c,d) in C Language

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.

逗号运算符:依次计算a和b以及c和d并返回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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM