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. 200 — Answer: 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…
- A.
100
- B.
200
- C.
300
- D.
None of these
Attempted by 318 students.
Show answer & explanation
Correct answer: B
Answer: 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 first element, so *arr is 0.
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.
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).