GATE CS 2025 Set 2 | Question 19

MCQ · 1 mark · −0.33

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?

  1. Option A: @#Hello World!
  2. Option B: Hello World!
  3. Option C: ello World!
  4. Option D: Hello World!d! (correct)

Answer

(D)

Explanation

The array holds the 14 characters @#Hello World! at indices 0 to 13, followed by '\0' at index 14.

stringcopy(a, a + 2) copies one character at a time from t to s, with t always two places ahead of s. Every character is therefore read before it can be overwritten, and the 12 characters Hello World! land in indices 0 to 11.

The loop stops as soon as *t is '\0', so the terminator is never copied. Indices 12 and 13 keep their old characters d!, and the string still ends at index 14, so printf prints Hello World!d!.

Answer: D