自增和自减运算符
#include <stdio.h>
int main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d %d %d", x, y, z);
}
输出:2 3 3
谁能解释一下吗?i =+ j
是什么意思(假设 i = 1
和 j = 2
)?
#include <stdio.h>
int main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d %d %d", x, y, z);
}
Output: 2 3 3
Can anyone explain this?
And what does i =+ j
mean (suppose i = 1
and j = 2
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
y = --x
表示“将 x 减 1,然后将结果存储在 y 中”z = x--
表示“保存 x 的温度。将 x 减 1将临时值存储在 z 中因此:
y = --x
means "decrease x by one, then store the result in y"z = x--
means "save a temp of x. Decrease x by one. Store the temp value in z"Hence:
后缀递减运算符 (x--) 返回变量递减之前的值。
两次。
它是 x 之后的值
从 4
将其分配给 x 的值
之前它从 3 减少。
The postfix decrement operator (x--) returns the value of the variable before it was decremented.
twice.
it to the value of x after it was
decremented from 4
assigned it to the value of x
before it was decremented from 3.
简单解释:
--x 或 ++x :之后会修改值。
之前修改
x-- 或 x++ :值将在详细说明:
--x 或 ++x:预减/ increment:会先进行自减或自增操作,然后再赋值x。
x-- 或 x++:post:decrement/increment:先给 x 赋值,然后再进行递减或递增操作。
让我们以更好的格式编写代码,并逐步浏览代码并对其进行注释以直观地向您展示发生的情况:
simple explanation:
--x or ++x : Value will be modified after.
x-- or x++ : Value will be modified before
Detailed explanation:
--x or ++x: pre-decrement/increment: will first do the operation of decrementing or incrementing first, then it will assign x.
x-- or x++: post:decrement/increment: will first assign the value of x and then it will do the operation of decrementing or incrementing after.
lets write your code in a nicer format and go through your code step by step and annotate it to show you visually what happens:
您必须了解后自减和预自减运算符的概念。
两者都会递减您的变量,但其中之一将返回原始值 (
x--
),其中之一将返回递减后的值 (--x
)。You have to understand the notions of post-decrement and pre-decrement operator.
Both will decrement your variable, but one of them will return the original value (
x--
) and one of them will return the decremented value (--x
).后缀自减 (x--) 与前缀自减 (--x) 不同。
前者返回值 x,然后将其递减;后者递减然后返回值。
如果您了解后缀是如何在低级别编写的,您会发现它比前缀慢一点......:)
Postfix decrement (x--) is different from prefix decrement (--x).
The former return the value x, then decrements it; the latter decrements and then returns the value.
And if you thing how a postfix is written at low level, you'll find that it is a liiiitle slower than the prefix... :)
X 递减,然后将 X 的值赋给 Y (3)
将 X 的值赋给 Z (3),将 X 递减 (2)
X is decremented, then Y is assigned with the value of X (3)
Z is assigned with the value of X (3), the X is decremented (2)
是:
x = 4
y = 预减x(基本上是减1然后赋值,即y = x-1 = 3
x =3
z = 减后 x(赋值后减 1,即 z = x = 3,则 x = x - 1
x = 2
所以 x = 2, y = 3, z = 3,正是您所看到的。
Yes:
x = 4
y = pre-decrement x (basically decrement by 1 and then assign, i.e. y = x-1 = 3
x =3
z = post-decrement x (decrement by 1 after assigning the value, i.e. z = x = 3, then x = x - 1
x = 2
So x = 2, y = 3, z = 3, exactly what you saw.
如果运算符是前缀,则增量发生在赋值之前,如果运算符是后缀,则增量发生在赋值之后。
If the operator is a prefix the incrementation happens before the assignment, if the operator is a postfix the incrementation happens after the assignment.
令
**
为递增/递减运算符。**e
表示将**
应用于e
并评估结果,而e** 表示评估
e
,然后对其应用**
。因此,如果减量和求值分开,则代码如下所示:
这将为您提供您所拥有的输出;)
let
**
be an increment/decrement operator.**e
means apply**
toe
and evaluate the result wherease**
means evaluatee
and then apply**
to it.Ergo, if decrementation and evaluation are seperated, the code reads as:
which gives you the ouput you have ;)
以下是您的示例中(大致)发生的情况:
预递增/递减在伪代码中看起来像这样
,后递增/递减看起来像这样
Here is what happens (roughly) in your example :
A pre increment/decrement looks like this in pseudocode
and a post increment/decrement looks like this
输出 2,3,3................................第一次 x=4 很好。 y=--x,表示x的值减1并存储在y中,因此现在y=3,x也是3。则z=x--表示x的值存储在z(z=3)中,并且然后 x 递减,即现在 x=2 但 z=3。当你打印值时, printf() 打印 2 3 3
output 2,3,3.................................first time x=4 fine. y=--x, means value of x is decremented by 1 and stored in y, thus now y=3 and x is also 3. then z=x-- means value of x is stored in z( z=3) and then x is decremented i.e now x=2 but z=3. when u r printing the value, then printf() prints 2 3 3
讨论
i=+j;
的含义(给定 i=1 和 j=2)i=+j;
相当于i=i+j;
所以你的表达式变成i=1+2
即i=3
Talking about what
i=+j;
means(given i=1 and j=2)i=+j;
is equivalent toi=i+j;
so your expression becomesi=1+2
i.e.i=3