edited by
27,480 views
70 votes
70 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 _______

edited by

13 Answers

Best answer
85 votes
85 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
54 votes
54 votes

I hope now you can understood easily

11 votes
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
Answer:

Related questions

30 votes
30 votes
11 answers
1
Madhav asked Feb 14, 2017
9,548 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 $...
25 votes
25 votes
7 answers
2
Madhav asked Feb 14, 2017
11,770 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 prog...
26 votes
26 votes
3 answers
4
khushtak asked Feb 14, 2017
5,768 views
Match the following:$$\begin{array}{|ll|ll|}\hline P. & \text{static char var ;} & \text{i.} & \text{Sequence of memory locations to store addresses} \\\hline Q. & \text...