• edited by
49,065 views
126 126 votes

Consider the following C program.

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

The output of the program is _______

17 Answers

Best answer
137 137 votes
char c[]="GATECSIT2017";
char *p=c;
printf("%d",strlen(c+2[p]-6[p]-1)); 

$2[p] = *(2+p) = p[2]$
$6[p] = *(6+p) = p[6]$
$c + 2[p] - 6[p] -1 = c + 'T' - 'I' - 1 = c + 11 - 1 = c + 10$ (In any character coding all alphabet letters are assigned consecutive int values as per C)

printf will print $2$ which is the length of "$17$".

• edited by
75 75 votes

I hope now you can understood easily

11 11 votes
The answer wil be 2.
This was asked , c+2[p]-6[p]-1
Look at 2[c] - 6[p] this will be evaluate as ( T -  I)

the difference in ascii walue of T and I will be 11.
So it will boil down to c + 11-1

means c+10  and the address for stringlength will become like strlength("17');

That is 2
6 6 votes

This question looks like a difficult one, but it’s actually rather simple if you know where to focus.

Like all GATE questions, simple questions are made intentionally obtuse with misdirections and useless ornaments.

 

This question tests your understanding of the function $strlen()$

$strlen()$ counts the character in a string (which is an array of characters in C) until it reaches the NULL character.

i.e, it returns the count of characters except for the NULL character.

 

Let’s evaluate: $c + 2[p] – 6[p] - 1$

  1. $c$  is the pointer to the head of the character array, i.e., a pointer to $c[0]$
     
  2. $2[p]$ is another way of writing $p[2]$ which is equilvalent to  $\text{‘T’}$ which in integer terms is $(65 + 19) = 84$
     
  3. $6[p]=p[6]= \text{‘I’}$ which in integer terms is $(65 + 8) = 73

 

$\therefore$ Solving the pointer arithmetic:

$(c + 84 – 73 - 1)$

$=(c + 10)$

which is the pointer to $\text{‘1’}$ (the 2nd last character)

$\therefore$ the call $strlen(c + 84 – 73 - 1) = strlen(\text{pointer to ‘1’})$

Since, $strlen()$ returns the count of chars till NULL is reached, it counts the chars $\{\text{‘1’, ‘7’}\}$ and returns $2$

 


 

NOTE:

So many useless statements in the question:

  1. $2[p]$ instead of $p[2]$
  2. $(int)$ typecast in printf which is unnecessary and just hides $strlen$ which is the main focus of this question

 

In programming questions, the trend seems to hide the important parts behind useless quirks of the C program, so if you're confused about what to focus on, you know where to look now.

Answer:
Position:
Show:

Related questions

48 48 votes
12 answers 12 answers
17.1k
17.1k views
Madhav asked Feb 14, 2017
17,147 views
Consider the following function implemented in C:void printxy(int x, int y) { int *ptr; x=0; ptr=&x; y=*ptr; *ptr=1; printf(“%d, %d”, x, y); }The output of invoking $prin...
47 47 votes
8 answers 8 answers
20.9k
20.9k views
Madhav asked Feb 14, 2017
20,937 views
Consider the following C program.#include<stdio.h int main () { int m=10; int n, n1; n=++m; n1=m++; n ; n1; n-=n1; printf(“%d”, n); return 0; }The output of the program ...
0 0 votes
1 answers 1 answer
1.2k
1.2k views
Bikram asked May 14, 2017
1,237 views
Suppose the following declarations are in effect :$\text{int }a[ \: ] = \{5,15,34,54,14,2,52,72 \}$;$\text{int }*p = \&a , *q = \&a[5]$;The value of $q - p$ is ________...