Consider the following C Program. #include #include 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 …
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2017 Set 2 · Q55
Time
#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 _________.Your answerNumerical answer
Solution
Official answer: 2 (±0)
Programming in CLet us analyze the pointer arithmetic and array indexing in C:
The string c is "GATECSIT2017" with 0-based indices:
c[0] = 'G'
c[1] = 'A'
c[2] = 'T'
c[3] = 'E'
c[4] = 'C'
c[5] = 'S'
c[6] = 'I'
c[7] = 'T'
c[8] = '2'
c[9] = '0'
c[10] = '1'
c[11] = '7'
c[12] = '\0'
The pointer points to the start of the string ().
In C, 2[p] is equivalent to *(2 + p), which equals p[2] = 'T'.
Similarly, 6[p] is equivalent to *(6 + p), which equals p[6] = 'I'.
Using their ASCII values:
Therefore, .
The pointer expression inside strlen is:
The pointer c + 10 points to the substring "17".
strlen("17") evaluates to .
Hence, the output printed is 2.
Try the question first, then check your work.
Examiner trap
In C, i[p] is identical to p[i]. Find the character values at indices 2 and 6, subtract their ASCII values, and determine the offset from pointer c.
This Programming in C previous year question is Q55 of the GATE CSE 2017 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2017 Set 2 has 6.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.