Predict the output of the following Code?

2019

Predict the output of the following Code?

image.png

Answer: B. 12 12ptr is initialized with a, so it points to the first element of the array, a[0]. Initially, a[0] is 10. The expression *ptr += 2 is equivalent to *ptr = *ptr…

  1. A.

    30 30

  2. B.

    12 12

  3. C.

    12 30

  4. D.

    10 20

Attempted by 51 students.

Show answer & explanation

Correct answer: B

ptr is initialized with a, so it points to the first element of the array, a[0]. Initially, a[0] is 10.

The expression *ptr += 2 is equivalent to *ptr = *ptr + 2. Therefore a[0] becomes 10 + 2 = 12, and the value of this assignment expression is also 12. The first printf prints 12.

The pointer has not moved; it still points to a[0]. So the second printf also prints 12.

Hence, the output is 12 12.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…