首页 > 代码库 > 奇怪的++操作

奇怪的++操作

#include <stdio.h>

int main(void)
{
    int j, i = 3;

    printf("j = %d\n", j = (++i) + (++i) + (++i));

    i = 3;
    printf("j = %d\n", j = (i++) + (i++) + (i++));
    i = 3;
    printf("j = %d\n", j = (++i) + (i++) + (++i));

    return 0;
}

/*   结果: * j = 16
 * j = 9
 * j = 13
 * */



#include <stdio.h>

#define SQUARE(x) ((x)*(x))
int main(void)
{
    int a = 5;
    int b, c;
   
    b = SQUARE(a++);
    a = 5;
    c = SQUARE(++a);
    
    printf("b: %d, c: %d\n", b, c);

    a = 5;
    b = (a++)*(a++);
    a = 5;
    c = (++a)*(++a);

    printf("b: %d, c: %d\n", b, c);

    a = 5;
    b = (a++)*(a++)*(a++);
    a = 5;
    c = (++a)*(++a)*(++a);

    printf("b: %d, c: %d\n", b, c);

    a = 5;
    b = (a++)*(a++)*(++a);
    a = 5;
    c = (++a)*(a++)*(++a);
    // 6*6*7 ??? 
    a = 5;
    c = (++a)*(a++);
    // 6*6 ???
    printf("b: %d, c: %d\n", b, c);


    return 0;
}

执行结果:

b: 25, c: 49
b: 25, c: 49
b: 125, c: 392
b: 150, c: 36


++ 表达式计算有什么规律?

编译器相关吗?

奇怪的++操作