GateAI
Programming in C

GATE CSE 2017 Set 2 · Q55

NAT2 marksMedium

Time

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 _________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 2 (±0)

Programming in C

Let us analyze the pointer arithmetic and array indexing in C:

  1. 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'

  2. The pointer pp points to the start of the string (p=cp = c).

  3. 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'.

  4. Using their ASCII values:

    • ASCII(′T′)=84\text{ASCII}('T^{\prime}) = 84

    • ASCII(′I′)=73\text{ASCII}('I^{\prime}) = 73

    • Therefore, 2[p]−6[p]=′T′−′I′=84−73=112[p] - 6[p] = 'T^{\prime} - 'I^{\prime} = 84 - 73 = 11.

  5. The pointer expression inside strlen is:

    c+2[p]−6[p]−1=c+11−1=c+10c + 2[p] - 6[p] - 1 = c + 11 - 1 = c + 10
  6. The pointer c + 10 points to the substring "17".

  7. strlen("17") evaluates to 22.

Hence, the output printed is 2.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

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 C

More from GATE CSE 2017 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2017 Set 2 · Q55 · NAT

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.