What will be output of the following program? #include<stdio.h> int main(){…

What will be output of the following program?

#include<stdio.h>

int main(){

int arr[]={0,10,20,30,40};

    int *ptr= arr;

    ptr=arr+2;

    printf("%d%d",*ptr,*arr);

    return 0;        

}

Answer: B. 200Answer: 200 (prints 20 followed by 0) Explanation: The array arr is initialized as {0, 10, 20, 30, 40}. The expression arr evaluates to the address of the…

  1. A.

    100

  2. B.

    200

  3. C.

    300

  4. D.

    None of these

Attempted by 318 students.

Show answer & explanation

Correct answer: B

Answer: 200 (prints 20 followed by 0)

Explanation:

  1. The array arr is initialized as {0, 10, 20, 30, 40}. The expression arr evaluates to the address of the first element, so *arr is 0.

  2. ptr = arr; then ptr = arr + 2 sets ptr to point to the third element of the array. That element has value 20, so *ptr is 20.

  3. The call printf("%d%d", *ptr, *arr); prints the two integers consecutively with no separator: first 20, then 0. These printed characters form "200".

Note: The original solution had a small typo showing printf with ptr instead of *ptr; the correct call in the code is printf("%d%d", *ptr, *arr).

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…