The GATE Grind

GATE 2017 CS (CS2) – Question 65

Programming and Data Structures · Programming in C · 2 marks · Numerical answer

Find the C program output.

#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;
}

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 2.0 to 2.0

Explanation

C subscripting is commutative: 2[p]=p[2]=T and 6[p]=p[6]=I. Under the paper’s ASCII assumption their codes differ by 84−73=11. The argument to strlen is c+11−1=c+10. The literal GATECSIT2017 has twelve characters, with indices ten and eleven holding 1 and 7, so the suffix is "17" and its length is 2.

More Programming in C questions

All Programming in C questions →