#include void main() { int arr[]={1, 2, 3, 4, 5}; int *p=arr; printf("%d",…

2024

#include
void main()
{
int arr[]={1, 2, 3, 4, 5};
int *p=arr;
printf("%d", *p++);
printf("%d", *(p+1));
}
Find the output of the above code?

  1. A.

    1, 2

  2. B.

    1, 3

  3. C.

    2, 3

  4. D.

    1, 4

Attempted by 304 students.

Show answer & explanation

Correct answer: B

Answer: 1, 3

Step-by-step explanation:

  • Initial state: p = arr, so p points to arr[0] which has value 1.

  • First printf: *p++ is parsed as *(p++). The dereference uses the original pointer value, so it prints 1. After evaluation, p is incremented and now points to arr[1].

  • Second printf: *(p+1) takes the current p (which points to arr[1]) and adds 1, so it accesses arr[2], whose value is 3.

  • Final output: 1, 3

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

Explore the full course: Mppsc Assistant Professor