当前位置:网站首页>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 !
边栏推荐
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- An example of HP array card troubleshooting
- 绝对定位时元素水平垂直居中
- ANOVA example
- 【JokerのZYNQ7020】DDS_ Compiler。
- 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
- Simple configuration of postfix server
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- Applet setting multi account debugging
猜你喜欢
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
IntelliJ 2021.3 short command line when running applications
大变局!全国房价,跌破万元大关
线程池:业务代码最常用也最容易犯错的组件
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
Kubernetes resource object introduction and common commands (III)
POM in idea XML graying solution
聊聊接口优化的几个方法
Leetcode: lucky number in matrix
PHP online confusion encryption tutorial sharing + basically no solution
随机推荐
简单配置PostFix服务器
kubernetes资源对象介绍及常用命令(四)
Leetcode: lucky number in matrix
One brush 149 force deduction hot question-10 regular expression matching (H)
RedHat 6.2 配置 Zabbix
Enterprise custom form engine solution (XI) -- form rule engine 1
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
[try to hack] active detection and concealment technology
LeetCode13.罗马数字转整数(三种解法)
Swm32 series Tutorial 4 port mapping and serial port application
SWM32系列教程4-端口映射及串口应用
Y is always discrete and can't understand, how to solve it? Answer: read it several times
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
The most complete postman interface test tutorial in the whole network, API interface test
1164 Good in C
C language modifies files by line
New library online | cnopendata complete data of Chinese insurance institution outlets
C language string inversion
Javescript variable declaration -- VaR, let, const
Apache服务挂起Asynchronous AcceptEx failed.