GATE | GATE CS 2019 | Question 33

Consider the following C program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


The number that will be displayed on execution of the program is _________ .
(A) 6
(B) 5
(C) 4
(D) segmentation error


Answer: (A)

Explanation:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Note that index of array always start from 0 in C.



Initially ip pointer is pointing at (arr+4) or skipping starting first 4 position.

Now in the printf system call, 1 more position is to skip, So it will point to (arr+5) or skip 5 position from starting:

Hence, printf will print value at 6th position, i.e., 6 will printed.

So, option (A) is correct.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.