自增和自减运算符

发布于 2024-09-26 08:09:44 字数 259 浏览 0 评论 0原文

#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 = 1j = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

甚是思念 2024-10-03 08:09:44

y = --x 表示“将 x 减 1,然后将结果存储在 y 中”

z = x-- 表示“保存 x 的温度。将 x 减 1将临时值存储在 z 中

因此:

  • x 从 4 开始。
  • 它会减少 1(到 3)。 3 存储在 y 中。
  • x 被保存到临时值。 x 再次减少(至 2)。然后将 temp (3) 存储在 z 中。
  • y 和 z 打印为 3,x 打印为 2。

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 starts at 4.
  • It gets decreased by 1 (to 3). 3 is stored in y.
  • x is saved to a temp. x is decreased again (to 2). then the temp (3) is stored in z.
  • y and z are printed as 3, x is printed as 2.
终止放荡 2024-10-03 08:09:44

后缀递减运算符 (x--) 返回变量递减之前的值。

  • x = 2,因为你已经减少了它
    两次。
  • y = 3,因为你已经分配了
    它是 x 之后的值
    从 4
  • z = 3 递减,因为你已经
    将其分配给 x 的值
    之前它从 3 减少。

The postfix decrement operator (x--) returns the value of the variable before it was decremented.

  • x = 2, because you've decremented it
    twice.
  • y = 3, because you've assigned
    it to the value of x after it was
    decremented from 4
  • z = 3, because you've
    assigned it to the value of x
    before it was decremented from 3.
北陌 2024-10-03 08:09:44

简单解释:

--x 或 ++x :之后会修改值。

之前修改

x-- 或 x++ :值将在详细说明:

--x 或 ++x预减/ increment:会先进行自减或自增操作,然后再赋值x。

x-- 或 x++post:decrement/increment:先给 x 赋值,然后再进行递减或递增操作。

让我们以更好的格式编写代码,并逐步浏览代码并对其进行注释以直观地向您展示发生的情况:

main() {
    //We declare the variables x, y and z, only x is given a value of 4.
    int x=4,y,z;

    //--x will decrement var x by 1 first THEN it will assign the value of x to y.
    //so: x = 3, y = 3 and z = nothing yet.
    y = --x;

    //x-- will assign the value of x to z first, then var x will be decremented by 1 after.
    //so: x = 2, y=3 and z = 3
    z = x--; 

    printf ("\n %d %d %d", x,y,z);

}

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:

main() {
    //We declare the variables x, y and z, only x is given a value of 4.
    int x=4,y,z;

    //--x will decrement var x by 1 first THEN it will assign the value of x to y.
    //so: x = 3, y = 3 and z = nothing yet.
    y = --x;

    //x-- will assign the value of x to z first, then var x will be decremented by 1 after.
    //so: x = 2, y=3 and z = 3
    z = x--; 

    printf ("\n %d %d %d", x,y,z);

}
情话难免假 2024-10-03 08:09:44

您必须了解后自减和预自减运算符的概念。

两者都会递减您的变量,但其中之一将返回原始值 (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).

请远离我 2024-10-03 08:09:44

后缀自减 (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... :)

浪漫之都 2024-10-03 08:09:44
y = --x;

X 递减,然后将 X 的值赋给 Y (3)

z = x--;

将 X 的值赋给 Z (3),将 X 递减 (2)

y = --x;

X is decremented, then Y is assigned with the value of X (3)

z = x--;

Z is assigned with the value of X (3), the X is decremented (2)

风吹雪碎 2024-10-03 08:09:44

是:

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.

稚气少女 2024-10-03 08:09:44

如果运算符是前缀,则增量发生在赋值之前,如果运算符是后缀,则增量发生在赋值之后。

If the operator is a prefix the incrementation happens before the assignment, if the operator is a postfix the incrementation happens after the assignment.

锦欢 2024-10-03 08:09:44

** 为递增/递减运算符。 **e 表示** 应用于 e 并评估结果,而 e** 表示评估e,然后对其应用**

因此,如果减量和求值分开,则代码如下所示:

int x=4,y,z;
x-=1;//3
y = x;//3
z = x;//3
x-=1;//2

这将为您提供您所拥有的输出;)

let ** be an increment/decrement operator. **e means apply ** to e and evaluate the result whereas e** means evaluate e and then apply ** to it.

Ergo, if decrementation and evaluation are seperated, the code reads as:

int x=4,y,z;
x-=1;//3
y = x;//3
z = x;//3
x-=1;//2

which gives you the ouput you have ;)

山田美奈子 2024-10-03 08:09:44
  • x++ / x-- 称为 post 递增/递减(...之后修改的值)
  • ++x / ++x 称为 < em>pre 递增/递减(之前修改的值...)

以下是您的示例中(大致)发生的情况:

int x=4,y,z;  // declare x=4, y=0, z=0
y = --x;      // decrement x then assign it to y (pre decrement) (y=x=3)
z = x--;      // assign x to z then decrement x (post decrement) (z=3 and x=2)
printf ("\n %d %d %d", x,y,z);  // output 2 3 3

预递增/递减在伪代码中看起来像这样

read value
increment/decrement value
write value
assign value

,后递增/递减看起来像这样

read value
assign value
increment/decrement value
write value
  • x++ / x-- is called post increment / decrement (value modified after...)
  • ++x / ++x is called pre increment / decrement (value modified before...)

Here is what happens (roughly) in your example :

int x=4,y,z;  // declare x=4, y=0, z=0
y = --x;      // decrement x then assign it to y (pre decrement) (y=x=3)
z = x--;      // assign x to z then decrement x (post decrement) (z=3 and x=2)
printf ("\n %d %d %d", x,y,z);  // output 2 3 3

A pre increment/decrement looks like this in pseudocode

read value
increment/decrement value
write value
assign value

and a post increment/decrement looks like this

read value
assign value
increment/decrement value
write value
子栖 2024-10-03 08:09:44
#include<stdio.h>
main ()
{
int x=4,y,z;
y = --x;
z = x--;
printf ("\n %d %d %d", x,y,z);
}

输出 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

#include<stdio.h>
main ()
{
int x=4,y,z;
y = --x;
z = x--;
printf ("\n %d %d %d", x,y,z);
}

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

蓝礼 2024-10-03 08:09:44

讨论 i=+j; 的含义(给定 i=1 和 j=2)

i=+j; 相当于 i=i+j; 所以你的表达式变成 i=1+2i=3

Talking about what i=+j; means(given i=1 and j=2)

i=+j; is equivalent to i=i+j; so your expression becomes i=1+2 i.e. i=3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文