X=8x-=--x-x--;printf("%d",x);that time the output of this is 6. but my things this output is 9. so please give me descriptionof this output(6).

Showing Answers 1 - 8 of 8 Answers

Hi Pankaj

The precedence order of the operators will be x-- , --x , - , =- respectively
As the first two operations are executed the expression
will be as follows;
x -= 7 - 7....(the x-- operation will be evaluated on the next line only)

Now the subtraction is done and the statement will be , x -= 0
Now -= operation will execute. ie x = x - 0 ie x = 7.

On the next line this value will be decremented by one because of x-- statement

Thus the value 6 will be printed..(7-1)

  Was this answer useful?  Yes

sirilux

  • Jan 11th, 2010
 

Some compilers may take values from left to right also..in that case
x=8
x-=--x-x--;
// --x is 7 and x-- is 7(latest values of x)
so 7-7 is 0
x-=0;
x=x-0(latest value of x is 6
so x is 6.

  Was this answer useful?  Yes

bjkrishna

  • Jul 5th, 2010
 

x-=--x-x--;

This is a special case.
Here --x and x-- are evaluated first and then final value of x is substituted in the expression regardless of precedence and associativity.
After --x, x becomes 7.
After x--, x becomes 6.

Now the final value in x is 6. This is substituted in the expression.
So 
x-=6-6
x-=0
x=x-0
which gives x=6.

  Was this answer useful?  Yes

krischan

  • Nov 23rd, 2010
 

--x is pre decrement so x becomes 7.
x-- is post decrement no need to worry about this until the expression is evaluated.
first x will have 7. second x will have 7. dont forget about post dec.
7-7 is 0
now the expression is x-=0
we can write it as x=7-0=>7
now the expression is evaluated totally. so post decrement will be done.
x--;x=7-1;
6

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions