Examveda
Examveda

What number will z in the sample code given below?
int z, x=5, y= -10, a=4, b=2;
z = x++ - --y*b/a;

A. 5

B. 6

C. 9

D. 10

E. 11

Answer: Option D

Solution(By Examveda Team)

C Operator Precedence Table
According to precedence table execution of the given operators are as follows:
1. x++(Postfix operator) i.e x will become 5
2. y--(Prefix operator) i.e y will become -11
3. * and / have same priority so they will be executed according to their associativity i.e left to right. So, *(Multiplication) will execute first and then /(division).
4. -(Subtraction)

So the complete expression would be
5 - (-11)*2/4 = 5 - (-22)/4 = 5 - (-5) = 5 + 5 = 10.


This Question Belongs to C Program >> Operators And Expressions

Join The Discussion

Comments ( 6 )

  1. Shubham Shinde
    Shubham Shinde :
    1 year ago

    z=x++ - --y*b/a
    1.first precedence increament oprator execute x++=6
    2.second precedence --y=-9
    3.3rd y*b because of in the precedence rule both * and / precedence are same here first come any one in both there are consider in this multiplication come because of y*b=-9*b=-18
    4.4th is b/a=-18/4=6.5 in the integer float value 6.5 consider as 6
    5.5th calculation=6- -9*2/4=10
    Thats solve.

  2. Sravya Palavalasa
    Sravya Palavalasa :
    5 years ago

    Precedence operators
    High priority * / %
    Low priority + -
    Z= 5++ - - -(-10)*2/4
    = 5++ - - - (-5)
    = 5+5
    =10

  3. Aman Kumar
    Aman Kumar :
    5 years ago

    x++(Postfix operator) i.e x will become 6 (not 5.)

  4. Samar Panda
    Samar Panda :
    6 years ago

    I can't understand totally.

  5. Anusha Makwana
    Anusha Makwana :
    6 years ago

    -22/4=5 how??
    -20/4=5 is right

  6. Sonu .s
    Sonu .s :
    7 years ago

    At step 2 it is mentioned as prefix but performed post fix? Explain how -11 is possible

Related Questions on Operators and Expressions