Consider the following C program:
#include <stdio.h>
int main() {
int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4;
printf(“%d\n”, ip[1]);
return 0;
}
The number that will be displayed on execution of the program is _______
View worked solution
Answer: 6
To find the output of the given C program, let's trace the array elements and pointer arithmetic step-by-step:
- Array Initialization:
The array
arris initialized with the following elements and their corresponding 0-based indices:Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 1 2 3 4 5 6 7 8 9 0 1 2 5 - Pointer Initialization:
The pointer
ipis initialized as:
In C, the array nameint *ip = arr + 4;arrdecays to a pointer to its first element (&arr[0]). Therefore,arr + 4points to the element at index 4, which isarr[4](value5). - Evaluating the Print Statement:
The program prints
ip[1]. Using the definition of array subscripting in C: Substitutingip = arr + 4into the equation: The value stored atarr[5]is6.
Thus, the number displayed on execution of the program is 6.
Free practice · Your answers on this page are not saved.
