What will be the output of the following ‘C’ code ? main ( ) { int x = 128;…

2014

What will be the output of the following ‘C’ code ?
 main ( )
 {   int x = 128;
     printf (“\n%d”, 1 + x ++);
 }

  1. A.

    128

  2. B.

    129

  3. C.

    130

  4. D.

    131

Attempted by 1258 students.

Show answer & explanation

Correct answer: B

Answer: 129

Explanation:

  • Evaluate the postfix increment x++: it yields the current value 128 for use in the expression, and then increments x to 129 after its value is taken.

  • Compute the addition: 1 + 128 = 129, which is the value passed to printf and therefore printed.

  • After the statement, x holds 129 because the postfix increment updated it.

Note: This behavior is well-defined—the postfix increment returns the original value and then increments, so there is no undefined behavior in this expression.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor