in Programming in C edited by
27,605 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

84 votes
84 votes
Best answer
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
by

4 Comments

ASCII value of ‘T ’ is: 84
ASCII value of ‘I’ is: 73

84 – 73 – 1 = 10
1
1

similar type of concept asked in GATE CSE 2011 | Question: 22

0
0
No need to remember ASCII code of alphabet character. we can simply write value of T in numeric number which is 20 and I numeric value which is 9
2
2
52 votes
52 votes

I hope now you can understood easily

4 Comments

How u took size of char as 1?? I mean they didn't mentioned 1 or 2 bytes??
0
0

generally, character is stored in terms of consecutive bytes. 

@G Shaheena

0
0

@Anup patel

%d is given not %s in the question??

0
0
33 votes
33 votes

Hope it might help.......

1 comment

1010 means from 10 th position consider complete string?
1
1
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

4 Comments

@Sourav Basu @Arjun  @Shaik Masthan (sorry for disturbing you )

sir the result is c+10 which (on considering c = 1000) gives 1010.

then shouldn't it be strlen(1010)   why are we taking element at that address as dereferencing operator is not used *(c+10) would give "17"  ...pls tell me where i am getting wrong.

0
0

@dharmesh7

read first concept of strlen function before attempting the question

https://www.geeksforgeeks.org/strlen-function-in-c/

1
1
@dharmesh7

same doubt please let know if u get it.

thanks
0
0
Answer:

Related questions