当前位置:网站首页>The difference between i++ and ++i: tell their differences easily
The difference between i++ and ++i: tell their differences easily
2022-07-03 17:22:00 【Meng Meng Mu Xi】
Recently, a friend asked me a question , The title is like this :
Assume that all variables are integers , Expression (x=2,y=5,y++,x+y) The value of is ( )
A. 7 B. 8 C.9 D. 2
Many people don't understand a problem , Because a lot of people ( Including teachers ) It's described like this :
i++ Is the assignment first , And then add up ;++i It's self increasing first , Post assignment
This will produce a A little misunderstanding . In this formula , expression y++, It is in this statement that +1 Well , Or in the next sentence +1?
The answer to this question is 8, choose B.
The reason is , I think it's understandable :
Programming Expressions usually have return values ,i++ And ++i No exception .
Such as :
int y = 3 + 2;You can put the 3 + 2 regard as expression , Calculation result assigned to y.
Empathy , We can understand it as :
i++ And ++i Can change variables i Self increasing 1, But before that :
i++ The expression increases by itself , The return value is i
++i The expression increases by itself , The return value is i+1
These two sentences are very important , If you use this conclusion to look at other topics , There is no difficulty in finding !
Such as :
int a = 1;
int b;
b = a++;
// b by 1because a++ The return value of a, Assigned to b. all b The value of is at this time a Value .
Such as :
int a = 1;
int b;
b = ++a;
// b by 2because a++ The return value of a+1, Assigned to b. all b The value of is at this time a+1 Value .
The same is true in the output , Such as :
int a = 1;
printf("%d\n",a++);
// Output 1because a++ yes printf The parameters of the function , So the output is a++ expression The return value of , That is to say a
Such as :
int a = 1;
printf("%d\n",++a);
// Output 2because ++a yes printf The parameters of the function , So the output is ++a expression The return value of , That is to say a+1
Of course , If no one uses their return value , Their effect Exactly the same :
Such as :
int a = 1;
a++;and
int a = 1;
++a;Now let's look at the problem at the beginning :
Assume that all variables are integers , Expression (x=2,y=5,y++,x+y) The value of is ( )
A. 7 B. 8 C.9 D. 2
Can be seen as y++ In the expression, but no one uses its return value .
So at the moment y++ And ++y It's exactly the same , It just did y+=1 It's just the operation of !
without doubt , This question 2 + 6 = 8 !
I still use C Functions of language simulation For a moment i++ and ++i Internal operations :
/**
* amount to i++ Function of
* @return i
*/
int iPlusPlus(int * i){
*i = *i + 1;
return *i;
}
/**
* amount to ++i Function of
* @return i+1
*/
int plusPlusI(int * i){
*i = *i + 1;
return *i+1;
}It's not easy to create , Give me a compliment if you like ! You are also welcome to point out mistakes in the comment area !
边栏推荐
- September, 19, "cam principle and application" online assignment [Full Score answer]
- Unity notes unityxr simple to use
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- AcWing 3438. Number system conversion
- [combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
- Talk about several methods of interface optimization
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- Take you to API development by hand
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- ANOVA example
猜你喜欢

Hongmeng third training

Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard

Notes on problems -- watching videos on edge will make the screen green

Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires

问题随记 —— 在 edge 上看视频会绿屏

1164 Good in C

Golang unit test, mock test and benchmark test

QT学习日记9——对话框
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
随机推荐
Leetcode13. Roman numeral to integer (three solutions)
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
Notes on problems -- watching videos on edge will make the screen green
Kubernetes resource object introduction and common commands (4)
A day's work list of an ordinary programmer
Redis: operation commands for list type data
POM in idea XML graying solution
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
IntelliJ 2021.3 short command line when running applications
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
新库上线 | CnOpenData中国保险机构网点全集数据
New library online | cnopendata complete data of Chinese insurance institution outlets
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
TensorBoard快速入门(Pytorch使用TensorBoard)
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
One brush 142 monotone stack next larger element II (m)
C语言按行修改文件
Solution to long waiting time of SSH connection to remote host
Swm32 series Tutorial 4 port mapping and serial port application