自加++

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

执行结果如下图:

原文地址:https://www.cnblogs.com/mu-tou-man/p/3928553.html