int main()
{
int a = (1,2,3);
int b = (++a,++a,++a);
int c= (b++,b++,b++);
printf("%d %d %d",a,b,c);
}
我是编程初学者.我没有得到这个程序如何显示6 9 8的输出.
解决方法
用于所有三个声明
int a = (1,3); int b = (++a,++a); int c = (b++,b++);
它是comma operator.它计算第一个操作数1并丢弃它,然后计算第二个操作数并返回其值.因此,
int a = ((1,2),3); // a is initialized with 3.
int b = ((++a,++a),++a); // b is initialized with 4+1+1 = 6.
// a is 6 by the end of the statement
int c = ((b++,b++),b++); // c is initialized with 6+1+1 = 8
// b is 9 by the end of the statement.
1在逗号运算符的情况下,从左到右保证评估顺序.