2.8k views

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 | 2.8k views
what was actual que . does this contain both printf line .......
Actual one had strlen wala .
i will just explain this wait for 5 min .....
"(int)" is it use for type casting???
very good question

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

sir i believe the question was a numerical answer type and had only one output which was to print just 2,

i remember that there was only one

printf statement which was

printf("%d",(int)strlen(c+2[p]-6[p]-1));

kindly change the question as it can be misleading

leave it for now. When the answer response comes, all the questions will be renetered. It should be coming next week.
why it is tycasted?
How do i 17 lenth ans is 2 Pls Explain ?
strangely counting position of alphabet number worked out here c is c[0]=g (7th position in alphabet) , similarly T=20th , I is 11th (7+20-11-1) :D , do you think it will work for every case ?
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
how c+10 become "17"?? kindly eplain
@pankajbelwal

(ascii value of c) + 10 (make sure ascii value of 'c' is relative to the value choosen in 't' and 'i' in previous steps )
Ascii value of C is not used here, C is the starting address of the array and C+10 is same as C[10], now strlen starts counting from the address passed to it as argument and stops when it finds '\0'(null- by default added at the end of any array of characters- string) but while counting array length strlen does not add extra 1 for this null character, so starting  from C+10, strlen finds that C+12 is null('\0), hence stops and returns length as 2

I hope now you can understood easily

how you start "c" address from 100. what need to do "110 %5"??? kindly plz explain...
you can assume any starting address for array . ANswer would be same .
tysm

Hope it might help.......

1010 means from 10 th position consider complete string?

output is 2