tags:

views:

170

answers:

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

Can anyone explain this?? And what does this : i=+j; means (suppose i = 1 and j =2)

+6  A: 

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.
Andy Thomas-Cramer
+3  A: 

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

Benoit
A: 

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

vulkanino
+9  A: 

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.
Jakob
-1: This is not precisely true. x-- means, "save a temp of x. Decrease x by one. Return the temp value". This distinction usually does not make a difference, but there are weird situations (usually involving poorly written code) where this behavior becomes relevant. It is also relevant if you are attempting to implement an increment operator yourself.
Brian
Ah, that's right. Correcting it now.
Jakob
+1 For corrected explanation. As an aside, this distinction is also relevant due to micro-optimizations (i.e. Where possible, preincrement is consdered better than post-increment since no temp is needed).
Brian
@Brian: For built-in types, a compiler is generally free to implement postincrement as "save value, increment, use temp", or "increment and use (new_value-1)", or "use value, then increment sometime later as convenient". Too bad no such facility was provided for user-defined post-increment operators.
supercat
Your explanation of `z = x++;` is misleading because it implies that C requires such a ridiculously inefficient procedure. A better explanation would be: "Store the value of `x` in `z`, then increment `x`." The side effect of postfix `++` should be thought of as happening at the next sequence point, not immediately mid-expression.
R..
@Supercat: This is true, but that is an optimization. Further, there are situations (involving poorly written code) where that optimization is not possible without changing the meaning of the code.
Brian
@Brian: If, between two sequence points, code uses a pre-increment or post-increment operator on a non-volatile variable and does anything else with that variable, the C standard allows the compiler to do //anything it wants//, up to and including working as expected, crashing, redirecting all your bookmarks at iLoveBarney.com, or anything else. If the behavior of someone's code would be affected by the changes I listed, the person should consider themselves lucky the compiler hasn't done something more "creative".
supercat
A: 
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)

Macmade
A: 

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.

Lazarus
A: 

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

typoknig
A: 

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

back2dos
A: 
  • 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
Yanick Rochon
+3  A: 

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

}
Pavan
OK i got it Thanks for the detailed description.I have one more thing: What does this means ; i=+j;
Ashutosh
that would not work, it means nothing. only += and others of its kind will work. += -= *= /= %= and so on.... they are assignment operators.y += x is equivelant as y = y + x. any more questions id be willing to help. although =+ works in perl. then again perl is crazy, it allows you to do anything. :D
Pavan
So what should it supposed to do if its allowed.
Ashutosh
sorry even in perl it doesnt work. It doesnt do anything. it will just give an error.
Pavan
= Normal Assignment, += Add and Assign, -= Subtract and Assign, *= Multiply and Assign, /= Divide and Assign, %= Modulus and Assign and **= Exponent and Assign.
Pavan
I was just going through some programming practice and i came across this and the question say it should work and asking for output.#include<stdio.h>main (){int i=1, j=1;for (;;){if (i>5)break;elsej+=i;printf ("\n%d", j);i=+j;}}
Ashutosh
where was this printed
Pavan
some stuff provided to me. Don't know which book it is from.
Ashutosh
yeah theres a mistake . the person that wrote it made a mistake. because that assignment operator doesnt exist and will not workyou have my word
Pavan
it should be either i += j... or i = i+ j. Just like in your code fragment it says j+=i so i believe its the same for the first one. So its most likely i += j instead of i =+j.
Pavan
Alright Thanks for looking so deep into this. I really appreciate it.
Ashutosh
"Koi gal nahi pagi" - No problem bro
Pavan
In old, *old* versions of C the compound assignment operators could take that form (http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V50_HTML/ARH9NATE/DOCU_026.HTM#3_1_5_), so the book might just be ancient. But in all likelihood, it's a typo.
Michael Burr
lol Thank you for the insight. I appreciate it.
Pavan
A: 

include

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

alfesani