[英]What's the meaning of if(a,b,c,d) in C Language
Possible Duplicate:
可能重复:
C++ Comma OperatorC ++逗号运算符
I had been looked if statement in C Language. 我一直在看用C语言编写if语句。 like this.
像这样。
if (a, b, c, d) {
blablabla..
blablabla..
}
What's the meaning of this if statement ? if语句的含义是什么?
What you have there is an example of the comma operator. 您所拥有的是一个逗号运算符的示例。 It evaluates all four expressions but uses
d
for the if
statement. 它计算所有四个表达式,但对
if
语句使用d
。
Unless the expressions other than d
have side effects (like a++
for example) they're useless. 除非
d
以外的表达式有副作用(例如a++
),否则它们是无用的。 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. i = 0
, j = 100
, i++
和j++
是两个完整表达式的组成部分,每个表达式都使用逗号运算符。
The relevant part of the standard is C11 6.5.17 Comma operator
: 该标准的相关部分是
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;
逗号运算符的左操作数被评估为void表达式; 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.
该函数具有三个参数,第二个参数的值为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
. 它计算一个,则b,则c,则d,并且使用的值
d
作为条件if
。 The other three are evaluated, but normally only for side-effects -- the results are discarded. 对其他三个进行评估,但通常仅针对副作用-丢弃结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.