当前位置:网站首页>i++ 和 ++i的真正区别
i++ 和 ++i的真正区别
2022-06-25 09:38:00 【QQ851301776】
创作人QQ:851301776,邮箱:[email protected],欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点!
个人座右铭:
1.没有横空出世,只要厚积一定发。
2.你可以学历不高,你可以不上学,但你不能不学习
一、i++和++i本质区别
核心区别点是:i++有中间保存的原因。
二、测试演练
#include <stdio.h>
int add1()
{
int i=0;
#if 1
printf("add1 = %d\n", (i++ + i++));
printf("i =%d\n", i);
i =0 ;
printf("add2 = %d\n", (i++ + ++i));
printf("i =%d\n", i);
#endif
i =0;
printf("add3 = %d\n", (++i + ++i));
printf("i=%d\n", i);
return 0;
}
int main(void)
{
int i = add1();
printf("main i= %d\n", (i++));
return 0;
}
[email protected]:/mnt/hgfs/project/visual_gateway/software/src_intra_20220618/test/v1/test/xx3/test$ ./a.out
add1 = 1
i =2
add2 = 2
i =2
add3 = 4
i=2
main i= 0
解析:
结合第一部分看:举例i++返回为temp, ++i返回i,只是为了区别
(1)printf("add1 = %d\n", (i++ + i++));
- 首先执行左边的i++,此时i=0,返回的结果temp1 = 0, 此时i的值为1。
- 其次执行右边的i++,此时i=1,返回的结果temp2=1, 此时i的值为2.
- 最后执行中间的加,为(temp1 + temp2)
(2)printf("add2 = %d\n", (i++ + ++i));
- 首先执行左边的i++,此时i=0,返回的结果为temp=0, 此时i的值为1。
- 其次执行右边的++i,此时i=1,返回的结果为i =2,此时i的值为2.
- 最后执行中间的加,为(temp + i)
(3)printf("add3 = %d\n", (++i + ++i));
- 首先执行左边的++i,此时i=0,返回的结果为i=1, 此时i的值为1。
- 其次执行右边的++1,此时i=1,返回的结果为i=2,此时i的值为2.
- 左边和右边都返回的i,此时的i都等于2
- 最后执行中间的加,为(i+i)= 4(这里需要主要的是:第一次返回的i之前等于1,现在已经变成了2)
(4)printf("main i= %d\n", (i++));
之前返回值i=0;
此时,结果也为0.
三、其他测试
1.demo
#include <stdio.h>
int add1()
{
int i=0;
printf("add1 = %d\n", (i++ + i++)); //打印等于1, i=2
printf("i =%d\n", i);
printf("add2 = %d\n", (i++ + ++i)); //打印等于6, i=4
printf("i =%d\n", i);
printf("add3 = %d\n", (++i + ++i)); //打印等于12, i=6
printf("i=%d\n", i);
return 0;
}
int main(void)
{
int i = add1();
printf("main i= %d\n", (i++));
return 0;
}
边栏推荐
- CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
- Reza RA series - development environment construction
- Best producer consumer code
- Simple waterfall effect
- Armbian version name comparison
- Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
- Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
- 链表 删除链表中的节点
- Opencv中的GrabCut图像分割
- The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
猜你喜欢
Minio基本使用与原理
Free applet making tool, how to make wechat applet
Methodchannel of flutter
Rxjs TakeUntil 操作符的学习笔记
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Jetpack compose layout (II) - material components and layout
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
I put a two-dimensional code with rainbow candy
Ruiji takeout project (II)
MongoDB的原理、基本使用、集群和分片集群
随机推荐
What functions should smart agriculture applet system design have
广发证券靠谱吗?是否合法?开股票账户安全吗?
ScheduleMaster分布式任务调度中心基本使用和原理
Where is safe for FTSE A50 to open an account
Minio基本使用与原理
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
[Ruby on rails full stack course] course directory
Neat Syntax Design of an ETL Language (Part 2)
js工具函数,自己封装一个节流函数
C语言刷题随记 —— 猴子吃桃
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
Match a mobile number from a large number of mobile numbers
How much money have I made by sticking to fixed investment for 3 years?
Rxjs TakeUntil 操作符的学习笔记
Computational Thinking and economic thinking
Shuttle JSON, list, map inter transfer
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function
MySQL创建给出语句
NFC read / write mode development - book summary