zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++学习——c++逗号操作符说明(附加全部运算符优先级)

C++学习 说明 运算符 全部 优先级 操作符 逗号
2023-06-13 09:11:03 时间

大家好,又见面了,我是你们的朋友全栈君。

逗号表达式

又称为“顺序求值运算符”。逗号表达式的一般形式为 (表达式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;
}

结果为:

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170957.html原文链接:https://javaforall.cn