环境:
ubuntu 13.04 32位
gcc version 4.7.3
++i,i++
代码如下:
#include <stdio.h> main() { int i; //test assignment int j; i=0; printf("before i++ assignment,i:%d ",i); j=i++; printf("after i++ assignment,i:%d j:%d ",i,j); i=0; printf("before ++i assignment,i:%d ",i); j=++i; printf("after ++i assignment,i:%d j:%d ",i,j); //test if i=1; printf("before if:%d ",i); if(i++){ printf("in if:%d ",i); } printf("out if:%d ",i); //test for i=0; printf("before for:%d ",i); for(;i<1;i++){ printf("in for:%d ",i); } printf("out for:%d ",i); }
结果如下:
before i++ assignment,i:0 after i++ assignment,i:1 j:0 before ++i assignment,i:0 after ++i assignment,i:1 j:1 before if:1 in if:2 out if:2 before for:0 in for:0 out for:1
分析:
自增运算符作为前缀和后缀的区别:
前缀指变量使用之前就已经获得了改变。后缀指变量在使用之后才会改变。
问题是:怎样才算是使用前,怎样才算是使用后?