Home / C Programming / Expressions :: Discussion

Discussion :: Expressions

  1. What will be the output of the program?

    #include
     int main() 
     {     
          int x=4, y, z;  
          y = --x;   
          z = x--;   
          printf("%d, %d, %d\n", x, y, z);   
          return 0; 
    } 
    

  2. A.

    4, 3, 3

    B.

    4, 3, 2

    C.

    3, 3, 2

    D.

    2, 3, 3

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    Step 1: int x=4, y, z; here variable x, y, z are declared as an integer type and variable x is initialized to 4.
    Step 2: y = --x; becomes y = 3; because (--x) is pre-decrement operator.
    Step 3: z = x--; becomes z = 3;. In the next step variable x becomes 2, because (x--) is post-decrement operator.
    Step 4: printf("%d, %d, %d\n", x, y, z); Hence it prints "2, 3, 3".


Be The First To Comment