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

 分析:

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

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

posted @ 2016-05-24 20:34  hu983  阅读(5858)  评论(0编辑  收藏  举报