学习 C语言第二天,今天是对前置++,--,后置++,--的介绍。
前置++,--:变量使用之前加一或减一
具体实现如下:
int main(void){
int i = 0, j = 10;
int temp;
//前置++
temp=++i;
printf("++before:");
printf("%d\n", temp);
printf("++after:");
printf("%d\n", i);
//前置--
temp=--j;
printf("--before:");
printf("%d\n", temp);
printf("--after:");
printf("%d\n", j);
return 0;
}
输出结果:
++before:1
++after: 1
--before: 9
--after: 9
后置++,--:在变量使用之后值加一或减一
具体实现:
int main(void)
{
int i = 0, j = 10;
int temp;
//后置++
temp=i++;
printf("++before:");
printf("%d\n", temp);
printf("++after:");
printf("%d\n", i);
//后置--
temp=j--;
printf("--before:");
printf("%d\n", temp);
printf("--after:");
printf("%d\n", j);
return 0;
}
{
int i = 0, j = 10;
int temp;
//后置++
temp=i++;
printf("++before:");
printf("%d\n", temp);
printf("++after:");
printf("%d\n", i);
//后置--
temp=j--;
printf("--before:");
printf("%d\n", temp);
printf("--after:");
printf("%d\n", j);
return 0;
}
输出结果:
++before:0
++after: 1
--before: 10
--after: 9
总结,虽然前置和获知的作用都是对变量进行加一或减一的作用,但是前置和后置的输出结果并不同,主要原因在于
二者之间加一减一的时间不同。前置++,--在使用变量之前便已经完成了自加,自减;而后置++,--在使用变量之后,其变量值才会加一或减一。