当前位置:网站首页>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 1
because 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 2
because 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 1
because 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 2
because ++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 !
边栏推荐
- [combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
- 手把手带你入门 API 开发
- C language modifies files by line
- 免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
- 问题随记 —— 在 edge 上看视频会绿屏
- [combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
- One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
- Notes on problems -- watching videos on edge will make the screen green
- 基于主机的入侵系统IDS
- POM in idea XML graying solution
猜你喜欢
POM in idea XML graying solution
线程池:业务代码最常用也最容易犯错的组件
Golang unit test, mock test and benchmark test
UE4 official charging resources, with a total price of several thousand
SWM32系列教程4-端口映射及串口应用
New library online | cnopendata complete data of Chinese insurance institution outlets
Take you to API development by hand
ANOVA example
Notes on problems -- watching videos on edge will make the screen green
QT adjust win screen brightness and sound size
随机推荐
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
Golang unit test, mock test and benchmark test
Wechat applet for the first time
鸿蒙第四次培训
图之深度优先搜索
问题随记 —— 在 edge 上看视频会绿屏
When absolutely positioned, the element is horizontally and vertically centered
Squid service startup script
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
The most complete postman interface test tutorial in the whole network, API interface test
The difference between get and post
An example of HP array card troubleshooting
Play with fancy special effects. This AE super kit is for you
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
UE4 official charging resources, with a total price of several thousand
Qt调节Win屏幕亮度和声音大小
Applet setting multi account debugging
新库上线 | CnOpenData中国保险机构网点全集数据
大消费企业怎样做数字化转型?
September, 19, "cam principle and application" online assignment [Full Score answer]