#include
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 ______.
Correct Answer :
Solution :
The correct answer is 111.
Let us analyze the step-by-step execution of the given C program to understand why the output is 111.
Step 1: Array Initialization
We declare an integer array of size 5:
int arr[5] = {30, 50, 10};
Since only three values are explicitly provided in the initializer list, the remaining elements of the array are automatically initialized to 0. Thus, the array elements are:
arr[0] = 30
arr[1] = 50
arr[2] = 10
arr[3] = 0
arr[4] = 0
Step 2: Pointer Initialization and Assignment
We declare an integer pointer ptr:
int *ptr;
Then we assign a value to ptr:
ptr = &arr[0] + 1;
Here, &arr[0] is the address of the first element (index 0). Adding 1 to this address moves the pointer to the next integer element in memory, which is &arr[1]. Therefore, ptr points to arr[1] (where value is 50).
Step 3: Assigning to variable a
a = *ptr;
Dereferencing ptr gives the value stored at arr[1], which is 50. So, .
Step 4: Incrementing the value pointed to by ptr
(*ptr)++;
The expression (*ptr)++ increments the value stored at the address pointed to by ptr. Since ptr currently points to arr[1], the value of arr[1] is incremented by 1.
So, arr[1] becomes .
Step 5: Incrementing the pointer
ptr++;
This increments the pointer itself, making it point to the next element in the array, which is arr[2]. Now, ptr points to arr[2], whose value is 10.
Step 6: Calculating the final expression and printing
printf("%d", a + (*ptr) + arr[1]);
Let's substitute the values of the components in the expression:
1. a is 50.
2. *ptr refers to the current value pointed to by ptr, which is arr[2], so its value is 10.
3. arr[1] has been modified to 51 (from Step 4).
Adding these values together:
Therefore, the program outputs 111.
Access expert-curated educational resources and study materials—completely free.
Create, conduct, and manage professional online assessments with Mindyard. Perfect for teachers and institutes.
Copyright © 2026 Mindyard. All Rights Reserved.