The GATE Grind

GATE 2025 CS (CS2) – Question 19

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

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. @#Hello World!
  2. Hello World!
  3. ello World!
  4. Hello World!d!

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (D) Hello World!d!

Explanation

The 12 characters "Hello World!" are copied over the first 12 positions, and the null terminator is not copied. The original last two characters 'd' and '!' remain, so the output is "Hello World!d!".