in Programming in C recategorized by
3,717 views
9 votes
9 votes

Consider the following $C$ function

#include<stdio.h>
int main(void)
{
    char c[]="ICRBCSIT17"
    char *p=c;
    printf("%s",c+2[p]-6[p]-1);
    return 0;
}

The output of the program is 

  1. $\text{SI}$
  2. $\text{IT}$
  3. $\text{T1}$
  4. $17$
in Programming in C recategorized by
by
3.7k views

1 comment

Ans: 17
0
0

2 Answers

16 votes
16 votes
Best answer

  • 2[p]= p[2]= R
  • 6[p]= p[6]= I
  • R- I = 9 // R is the 9th letter after I
  • Thus c+ 2[p]-6[p] -1 = c+9-1 = c+8 

output will be 17

Answer D

selected by
by
6 votes
6 votes

17 is the answer.  

In C, there is a rule that whatever character code be used by the compiler, codes of all alphabets and digits must be in order. So, if character code of 'A' is x, then for 'B' it must be x+1.  

Now %s means printf takes and address and prints all bytes starting from that address as characters till any byte becomes the code for '\0'. Now, the passed value to printf here is 
p + p[2] - p[6] 

p is the starting address of array c. p[2] = 'R' and p[6] = 'I'. So, p[2] - p[6] = 9, and  9-1 =8 ,    p + 8 will be pointing to the eighth position in the array c. So, printf starts printing from 1 and prints 17.  

(Here "ICRBCSIT17" is a string literal and by default a '\0' is added at the end of it by the compiler).  

NB: In this question %s is not required. 

 printf(p + p[2] - p[6] - 1);

Also gives the same result as first argument to printf is a character pointer and only if we want to pass more arguments we need to use a format string.  
 

2 Comments

Please explain
 R- I = 9 // R is the 9th letter after I
Thus c+ 2[p]-6[p] -1 = c+9-1 = c+8
0
0
In ASCII R-I=9
0
0
Answer:

Related questions