C ++什么是'int x =(anyInt1,anyInt2);'意思? [英] C++ What does 'int x = (anyInt1, anyInt2);' mean?
问题描述
可能重复:
为什么3,758,096,384< 1给出768
今天我发现下面的代码用gcc编译: / p>
Today I found out that following code compiles with gcc:
#include <iostream>
int main()
{
int x = (23,34);
std::cout << x << std::endl; // prints 34
return 0;
}
为什么要编译? <...> ...
Why does this compiles? What is the meaning of (..., ...)?
推荐答案
在C ++中, 是一个运算符,因此
(23,34)
是一个类似于(23 + 34)
是一个表达式。在前者中,,
是运算符,而在后者中, +
是运算符。
In C++, ,
is an operator, and therefore (23,34)
is an expression just like (23+34)
is an expression. In the former, ,
is an operator, while in the latter, +
is an operator.
所以表达式(23,34)
计算到最右边的操作数 34
这就是为什么你的代码输出 34
。
So the expression (23,34)
evaluates to the rightmost operand which is 34
which is why your code outputs 34
.
,
不是函数调用中的运算符:
I would also like to mention that ,
is not an operator in a function call:
int m = max(a,b);
这里,
它不作为运算符。
Here ,
acts a separator of arguments. It doesn't act as operator. So you pass two arguments to the function.
但是,
int m = max((a,b), c);
这里首先,
second ,
是一个分隔符。所以你仍然传递两个参数到函数,而不是三个,它等价于:
Here first ,
is an operator, and second ,
is a separator. So you still pass two arguments to the function, not three, and it is equivalent to this:
int m = max(b, c); //as (a,b) evaluates to b
希望有帮助。 : - )
Hope that helps. :-)
这篇关于C ++什么是'int x =(anyInt1,anyInt2);'意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!