【发布时间】:2021-10-04 05:52:53
【问题描述】:
下面的表达式:-
int main()
{
int x=2, y=9;
cout << ( 1 ? ++x, ++y : --x, --y);
}
给出以下输出:-
9
根据我的理解,它应该返回 ++y,应该是 10。出了什么问题?
【问题讨论】:
-
不清楚你为什么期望 ++y 作为输出。我可以理解 --y,但 ++y 是条件的一部分,而不是输出
-
这样写:
(1 ? ++x, ++y : (--x, --y))。这应该可以满足您的需求。正如当前答案所示,这里的分组有点特殊。 -
@463035818_is_not_a_number:OP 期望
1 ? (++x, ++y) : (--x, --y)... -
@Jarod42 哦,好吧,我完全误读了条件。这是编写不可读代码的好方法;)
标签: c++ operator-precedence conditional-operator prefix-operator