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