Skip to content
GATEiit

GATE CSE 2017 Session 2 Q65: strlen with pointer arithmetic and i[p] array indexing

Syllabus topic: Programming in C

Question

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

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 2 (official GATE 2017 final answer key)

Concept used: In C, 2[p] means ∗(2+p), the same as p[2]; characters behave as integers (their ASCII codes) in pointer arithmetic.

Concept notes

Loading notes…

First step: Find the characters p[2] and p[6] in "GATECSIT2017", and use the difference of their ASCII codes to work out where in the string strlen starts counting.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Programming in C

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2017 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key