in Programming in C
1,621 views
3 votes
3 votes
#include<stdio.h>
#include<string.h>
int main(void) {
char *a="MADEEASY";
char *b="GATECSIT2019";
char *r=a;
char *s=b;
printf("%d",(int)strlen(b+3[r]-1[s]));
return 0;
}
 

Ans is given $8.$ I know basic thing, but couldnot getting what strlen(b+3[r]-1[s]) returning?? Plz explain.

in Programming in C
by
1.6k views

10 Comments

moved by
@amitqy

 

What is the answer?

Im getting 6
0
0
Is it 8?
0
0
yes.how printf statement is working
0
0
its 8
0
0
Here we need to see the execution steps to understand how it is 8 :-

strlen(b + 3[r] - 1[s])

what is 3[r] ? it is *(r+3) which is  E.

and 1[s] is *(1+s) which is A.

Here subtraction of characters give 4 since E-A is 4 (positional difference in ASCII).

now b + 4 will be considered from position 4 .

strlen('CSIT2019') is 8
4
4
char *a =  "GATE";

printf("%s",a)--->outputs GATE

printf("%d",a)--->outputs starting starting address of string "GATE"

correct?
0
0
8 is answer
0
0
Correct
0
0
8 is the correct answer
0
0

3 Answers

7 votes
7 votes
Best answer

strlen(b+3[r]-1[s]) :

first: 3[r] is same as r[3] i.e., 'E'

and 1[s] is same as s[1] i.e., 'A'

3[r] - 1[s] = 'E' - 'A' = 4

now, b + 4 refers to  4 th index in b i.e, 'C' (starting position)

strlen(b+4) = strlen("CSIT2019") = 8

Thus, it will return 8.

correct me if i am wrong.

selected by

3 Comments

But Arithmetic operator is Left to right associativity.

so why you evaluate the right first ?
0
0
yes, but it won't differ.
0
0
b+3[r]-1[s]=b+*(r+3)-*(s+1)

                   =b+E-A

                    =b+ 69 -65

                     =b+4

                       =  strlen(b+4)

                         =strlen(CSIT2019)

                          =8   { ASCII of A=65 &E=69}
0
0
0 votes
0 votes
b+3[r]-1[s]=b+*(r+3)-*(s+1)

                   =b+E-A      {ASCII OF A=65 &E=69}

                    =b+ 69 -65

                     =b+4

                       =  strlen(b+4)

                         =strlen(CSIT2019)

                          =8               (So answer is 8)
0 votes
0 votes
b+3[r]-1[s]=b+*(r+3)-*(s+1)    (r[3]=*(r+3)=*(3+r)=3[r])

                   =b+E-A      {ASCII OF A=65 &E=69}

                    =b+ 69 -65

                     =b+4 (here b points to C right now)

                       =  strlen(b+4)

                         =strlen(CSIT2019) (strlen never count \0')

                          =8               (So answer is 8)

Related questions