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 _______
View worked solution
Answer: 2
To find the output of the program, we evaluate the expression inside the strlen() function step-by-step:
The pointer c and p both point to the string literal "GATECSIT2017". The character elements and their indices are:
p[2](or2[p]) ='T'(ASCII value = 84)p[6](or6[p]) ='I'(ASCII value = 73)
Substituting these values into the pointer arithmetic expression:
The pointer c + 10 points to the character at index 10 of the string "GATECSIT2017", which is "17".
Thus, strlen(c + 10) calculates the length of the substring "17", which is 2.
Free practice · Your answers on this page are not saved.
