当前位置:网站首页>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;
}创作不易,喜欢的话给个赞趴!有错误也欢迎在评论区指出奥!
边栏推荐
- [UE4] brush Arctic pack high quality Arctic terrain pack
- Vs code plug-in korofileheader
- Leetcode: lucky number in matrix
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
- 一位普通程序员一天工作清单
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- 線程池:業務代碼最常用也最容易犯錯的組件
- How to train mask r-cnn model with your own data
- One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
猜你喜欢

2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)

The most complete postman interface test tutorial in the whole network, API interface test

Test your trained model

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you

The largest matrix (H) in a brush 143 monotone stack 84 histogram

1164 Good in C

One brush 147-force deduction hot question-4 find the median of two positive arrays (H)

TensorBoard快速入门(Pytorch使用TensorBoard)
![Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)](/img/be/4ef38f711e7319a2cc83db2bee3a07.jpg)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)

Unity notes unityxr simple to use
随机推荐
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
One brush 148 force deduction hot question-5 longest palindrome substring (m)
The largest matrix (H) in a brush 143 monotone stack 84 histogram
鸿蒙第四次培训
Squid service startup script
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
What is your income level in the country?
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
Redis: operation commands for list type data
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
Qt调节Win屏幕亮度和声音大小
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
RedHat 6.2 配置 Zabbix
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
Hongmeng fourth training
Redis:关于列表List类型数据的操作命令
New library online | cnopendata China bird watching record data