一 自增和自减
自增(++):就是给自己的值再加1
自减(–):就是给自己的值减1
tips: ++(–)可以放在前面,也可以放在后面,效果都是一样,都是会给自身+1(-1)
前缀++(–):会先自增(自减),后用自增(自减)后的值参与运算
后缀++(–):会先用原来的值参与运算,再自增(自减)
tips:代码例子如下
#include <stdio.h>
int main(int argc, const char * argv[]) {
//前缀++
/*
int num1 = 10;
int num2 = ++num1 + 2;
printf(“num1=%d num2=%d/n”,num1,num2);//11 13
*/
//后缀++
/*
// int num1 = 10;
//
// int num2 = num1++ + 2;
//
// printf(“num1=%d num2=%d/n”,num1,num2);//11 12
*/
//后缀–
/*
int num1 = 10;
int num2 = num1– + 3;
printf(“num1=%d num2=%d/n”,num1,num2);//9 13
*/
//前缀–
/*
int num1 = 10;
int num2 = –num1 + 3;
printf(“num1=%d num2=%d/n”,num1,num2);//9 12
*/
//++在后面
/*
int age = 23;
age++; //相当于 age+= 1 ++的意思是在自己值的基础上+1
printf(“age=%d/n”,age);//24
age++;
printf(“age=%d/n”,age);//25
*/
//–在后面
/*
int age = 14;
age–;
printf(“age=%d/n”,age);//13
age–;
printf(“age=%d/n”,age);//12
*/
//++在前面
/*
int age = 23;
++age;
printf(“age=%d/n”,age);//24
++age;
printf(“age=%d/n”,age);//25
*/
//–在前面
/*
int age = 14;
–age;
printf(“age=%d/n”,age);//13
*/
//前缀和后缀的练习
/*
// int num1 = 10;
//
// int num2 = num1++ + 3;
//
// printf(“num1=%d num2=%d/n”,num1,num2);//11 13
// int num1 = 10;
//9 – 10
// int num2 = –num1 – 10;
// printf(“num1=%d num2=%d/n”,num1,num2);//9 -1
// int num1 = 10;
// int num2 = 20;
// //10 + 21
// int num3 = num1++ + ++num2;
// printf(“num1=%d num2=%d num3=%d/n”,num1,num2,num3);//11 21 31
int num1 = 10;
int num2 = 20;
//9 + 20
int num3 = –num1 + num2++;
printf(“num1=%d num2=%d num3=%d/n”,num1,num2,num3);//9 21 29
*/
return 0;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13536.html