自加++

來源:互聯網
上載者:User

標籤: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");}

執行結果如:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.