Skip to content
GATEiit

GATE CSE 2025 Session 2 Q19: Overlapping string copy without the terminating null

Question

Consider the following C program:

#include <stdio.h>
void stringcopy(char *, char *);
int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
}
void stringcopy(char *s, char *t) {
    while(*t)
        *s++ = *t++;
}

Which ONE of the following will be the output of the program?

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation

Answer: D (official GATE 2025 final answer key)

Concept used: The loop stops at the null character without copying it, so the old tail of the array survives.

First step: The 12 characters "Hello World!" are copied two places to the left; the last two original characters "d!" and the original '\0' stay in place.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

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