C/C++中逗号表达式的用法


代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main(){
 7 
 8     int t1,t2;
 9     t1 = 2,t2++;
10     cout<<t1<<" "<<t2<<endl;
11     t1 = (5,6); //必须要加括号否则编译出错
12     cout<<t1<<endl;
13 
14     return 0;
15 }

输出:

2 1
6

 分析:

逗号运算符优先级最低,从左到右执行。

需要注意的是第九行代码实际上是两条表达式。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM