当前位置:网站首页>i++与++i的区别:通俗易懂的讲述他们的区别
i++与++i的区别:通俗易懂的讲述他们的区别
2022-07-03 17:21:00 【萌萌沐兮】
最近有一位朋友问我一道题,题目是这样的:
假设所有变量均为整型,则表达式(x=2,y=5,y++,x+y)的值是( )
A. 7 B. 8 C.9 D. 2
很多人不明白一个问题,因为很多人(包括老师)是这样描述的:
i++是先赋值,然后再自增;++i是先自增,后赋值
这就会产生一个小小的误区。在这个式子中,表达式y++,是在这个语句中就+1呢,还是说下一个语句中再+1?
这题的答案是8,选B。
究其原因,我觉得可以这样理解:
编程中的表达式一般情况是会有返回值的,i++与++i也不例外。
如:
int y = 3 + 2;
就可以把 3 + 2 看作表达式,计算结果赋值给 y。
同理,我们就可以理解为:
i++与++i都能将变量i自增1,但是在此之前:
i++ 表达式在自增的同时,返回值为 i
++i 表达式在自增的同时,返回值为 i+1
这两句话非常重要,如果用这个结论再看其它的题目时,发现毫无难度!
如:
int a = 1;
int b;
b = a++;
// b 为 1
由于a++的返回值是a,赋值给了b。所有b的值是此时a的值。
如:
int a = 1;
int b;
b = ++a;
// b 为 2
由于a++的返回值是a+1,赋值给了b。所有b的值是此时a+1的值。
在输出也一样,如:
int a = 1;
printf("%d\n",a++);
// 输出1
由于a++是 printf 函数的参数,所以输出的是 a++表达式 的返回值,也就是a
如:
int a = 1;
printf("%d\n",++a);
// 输出2
由于++a是 printf 函数的参数,所以输出的是 ++a表达式 的返回值,也就是a+1
当然,如果没有人使用到他们的返回值的时候,他们的效果完全相同:
如:
int a = 1;
a++;
和
int a = 1;
++a;
现在再看看刚开始的那道题:
假设所有变量均为整型,则表达式(x=2,y=5,y++,x+y)的值是( )
A. 7 B. 8 C.9 D. 2
可以看作 y++ 在表达式中但是没有人使用到他的返回值。
所以此刻 y++ 与 ++y 是完全相同的,它仅仅做了 y+=1 的操作罢了!
毫无疑问,这题 2 + 6 = 8 !
我还用C语言的函数模拟了一下 i++ 和 ++i 内部的操作:
/**
* 相当于i++的函数
* @return i
*/
int iPlusPlus(int * i){
*i = *i + 1;
return *i;
}
/**
* 相当于++i的函数
* @return i+1
*/
int plusPlusI(int * i){
*i = *i + 1;
return *i+1;
}
创作不易,喜欢的话给个赞趴!有错误也欢迎在评论区指出奥!
边栏推荐
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
- 手把手带你入门 API 开发
- Javescript variable declaration -- VaR, let, const
- Redis: operation commands for list type data
- 在iptables防火墙下开启vsftpd的端口
- Stm32h7 Hal library SPI DMA transmission has been in busy solution
- TensorBoard快速入门(Pytorch使用TensorBoard)
- Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
- Life is still confused? Maybe these subscription numbers have the answers you need!
- Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
猜你喜欢
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
问题随记 —— 在 edge 上看视频会绿屏
Simple use of unity pen XR grab
Analysis of variance summary
kubernetes资源对象介绍及常用命令(四)
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
SWM32系列教程4-端口映射及串口应用
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
[try to hack] active detection and concealment technology
随机推荐
基于主机的入侵系统IDS
C language modifies files by line
Depth first search of graph
vs2013已阻止安装程序,需安装IE10
Simple configuration of postfix server
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
Rsync远程同步
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
Kubernetes resource object introduction and common commands (4)
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
新库上线 | CnOpenData中国观鸟记录数据
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
Hongmeng third training
[combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
The most complete postman interface test tutorial in the whole network, API interface test
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
Open vsftpd port under iptables firewall
[RT thread] NXP rt10xx device driver framework -- pin construction and use