当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Wearable devices may reveal personal privacy

2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?

Data-driven anomaly detection and early warning of 21 May Day C

Remove the mosaic, there's a way, attached with the running tutorial

链表 删除链表中的节点

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

Why should the terminal retail industry choose the member management system

Vscode attempted to write the procedure to a pipeline that does not exist

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points

Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
随机推荐
Kotlin Foundation
瑞萨RA系列-开发环境搭建
manhattan_ Slam environment configuration
[MySQL learning notes 22] index
[MySQL learning notes 21] storage engine
链表 删除链表中的节点
manhattan_slam环境配置
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
NFC read / write mode development - book summary
Mysql 源码阅读(二)登录连接调试
Remove the mosaic, there's a way, attached with the running tutorial
Guiding principle - read source code
Jetpack compose layout (III) - custom layout
Jetpack compose layout (I) - basic knowledge of layout
How to "transform" small and micro businesses (II)?
Shuttle JSON, list, map inter transfer
MySQL create given statement
Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
8. Intelligent transportation project (1)