Increment and Decrement Operators

2019-09-19 20:06发布

问题:

#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)?

回答1:

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);

}


回答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.


回答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.


回答4:

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).



回答5:

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... :)



回答6:

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)



回答7:

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.



回答8:

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



回答9:

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 ;)



回答10:

  • 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


回答11:

#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



回答12:

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



标签: c increment