in Programming in C edited by
27,611 views
69 votes
69 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 _______

in Programming in C edited by
by
27.6k views

4 Comments

what would be the answer if we use sizeof() instead of strlen?

as the expression will evaluate to address so the sizeof() will give sizeof pointer (4 bytes in my machine)
0
0
Please note here that you don’t really need to remember the ASCII values of T or I. Why?

Because you just have to know the number of alphabetical difference between the two characters T & I. The difference is simply 11 (you can even count it on your fingers). Assuming A → 1; B → 2; C → 3;….. Z → 26

I.e; what’s the difference between G & D ?
‘G’ – ‘D’:   7 – 4 = 3

This intuition would come in handy in the exam environment.
1
1

Key concepts to note:

  1. 2[p] = *[p+2] = p[2]
  2. 6[p] = *[p+6] = p[6]

%s of (c+10) is 17, strlen in int will be 2.

0
0

13 Answers

0 votes
0 votes
1st concept is -Strlen does not count null pointer .

Now strlen(17) is 2.
Answer:

Related questions