Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q55 — Programming in C

Explain with AI

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] (or 2[p]) = 'T' (ASCII value = 84)
  • p[6] (or 6[p]) = 'I' (ASCII value = 73)

Substituting these values into the pointer arithmetic expression:

Expression=c+2[p]−6[p]−1=c+’T’−’I’−1=c+84−73−1=c+11−1=c+10\begin{aligned} \text{Expression} &= c + 2[p] - 6[p] - 1 \\ &= c + \text{'T'} - \text{'I'} - 1 \\ &= c + 84 - 73 - 1 \\ &= c + 11 - 1 \\ &= c + 10 \end{aligned}

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.

Published
Report a correction
Keep exploringMore Programming in C questions