標籤:style blog http color 使用 os io 2014
i++使用後加1
++i使用前加1
那麼 int a=++i + ++i;
是怎麼樣計算的呢?
++i;
++i;
a=i+i=2+2=4;
而i++是如何計算的呢
a=i++ + i++;
先執行a=i+i;
然後i++;
i++;
讓我們看下面的代碼
// 例1#include <iostream>#include <cstdlib>using namespace std;void main(){ int c = 0, a = 0, b = 0; cout<<"兩次次使用 ++a"<<endl; c = (++a) + (++a); cout<<"the value of c is "<<c<<endl; c = a + a; cout<<"the value of c is "<<c<<endl<<endl; cout<<"兩次次使用 b++"<<endl; c = (b++) + (b++); cout<<"the value of c is "<<c<<endl; c = b + b; cout<<"the value of c is "<<c<<endl<<endl; a = 0;//先重新初始化下 變數a cout<<"a++ 與 ++a 的混合使用, a++在前"<<endl; c = (++a) + (a++); cout<<"the value of c is "<<c<<endl; c = a + a; cout<<"the value of c is "<<c<<endl<<endl; a = 0;//先重新初始化下 變數a cout<<"a++ 與 ++a 的混合使用, a++在後"<<endl; c = (a++) + (++a); cout<<"the value of c is "<<c<<endl; c = a + a; cout<<"the value of c is "<<c<<endl<<endl; a = 0;//先重新初始化下 變數a cout<<"一個 a++ 與 兩個 ++a 的混合使用"<<endl; c = (a++) +(++a) + (++a); cout<<"the value of c is "<<c<<endl; c = a + a + a; cout<<"the value of c is "<<c<<endl<<endl; a = 0;//先重新初始化下 變數a cout<<"兩個 a++ 與 一個 ++a 的混合使用"<<endl; c = (a++) + (a++) + (++a); cout<<"the value of c is "<<c<<endl; c = a + a + a; cout<<"the value of c is "<<c<<endl<<endl; system("pause");}
執行結果如: