又称为“顺序求值运算符”。逗号表达式的一般形式为
(表达式1,表达式2,表达式3……表达式n)
求解过程是:先求解表达式1,再求解表达式2,…。整个逗号表达式的值是最后一个表达式n的值。
例如这里的“i++,p++”,先求i++的值,然后求p++的值,整个表达式的值是p++的运算结果
另外、逗号运算符是所有运算符中级别最低的
/*********************************************/
像表达式1中有后++ ,在遇到逗号之前也要算完,遇逗号就算一个表达句。
/*********************************************/
#include<iostream>
using namespace std;
int main()
{
int x, y, z;
x = y = 5;
cout << (x++, y++) << endl;
cout << x << endl;
cout << y << endl;
cout<< "****************************" <<endl;
cout << (--x, --y) << endl;
cout << x << endl;
cout << y << endl;
cout << "****************************" << endl;
cout << (z = x++, y++, --y, y + x) << endl;
cout << x << endl;
cout << y << endl;
cout << z << endl;
return 0;
}
联系客服