recategorized by
3,706 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$
recategorized by

2 Answers

Best answer
16 votes
16 votes

  • 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
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.  
 

Answer:

Related questions

3 votes
3 votes
2 answers
1
gatecse asked Dec 17, 2017
1,631 views
Consider the functionint func(int num) { int count=0; while(num) { count++; num>>=1; } return(count); }For $func(435)$ the value returned is$9$$8$$0$$10$
2 votes
2 votes
1 answer
2
gatecse asked Dec 17, 2017
2,116 views
Consider the functionint fun(x: integer) { If x>100 then fun=x-10; else fun=fun(fun(x+11)); }For the input $x=95$, the function will return$89$$90$$91$$92$
5 votes
5 votes
1 answer
3
gatecse asked Dec 17, 2017
1,432 views
Which one of the following are essential features of object-oriented language? A.Abstraction and encapsulation B.Strictly-typed C.Type-safe property coupled with sub-type...
9 votes
9 votes
1 answer
4