Consider the following C program: #include <stdio.h> int main(){ int a; int…
2025
Consider the following C program:
#include <stdio.h>
int main(){
int a;
int arr[5] = {30, 50, 10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}
The output of the above program is ___________. (Answer in integer)
Attempted by 119 students.
Show answer & explanation
Correct answer: 111
Final output: 111
Explanation of each step:
Initial array values: arr[0] = 30, arr[1] = 50, arr[2] = 10, arr[3] = 0, arr[4] = 0.
ptr is set to &arr[0] + 1, so ptr points to arr[1].
a = *ptr assigns the value at arr[1] to a, so a = 50.
(*ptr)++ increments the value at arr[1], changing arr[1] from 50 to 51.
ptr++ advances ptr to point to arr[2], whose value is 10.
The printed expression is a + (*ptr) + arr[1] = 50 + 10 + 51 = 111.