Skip to content
Official PYQ · 2025

GATE CS 2025 Set 2 Q9 — Programming in C

Explain with AI

​​​​​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?

Choose one option
View worked solution

Answer: D

Let's trace the execution of the program step-by-step:

1. Initialization:
The character array a is initialized as follows:

Index01234567891011121314
Value@#Hello World!\0

2. Function Call:
Calling stringcopy(a, a+2) sets the pointer s to a[0] and t to a[2].

3. Loop Execution:
The loop while (*t) *s++ = *t++; copies characters from source t to destination s one by one:

  • The characters 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' (from indices 2 to 13) are copied to indices 0 to 11.
  • When t points to a[14] (which is '\0'), the loop condition while (*t) evaluates to false and the loop terminates.
  • Because the loop terminates before copying, the null-terminator '\0' is not copied to a[12].

4. Final State of Array a:
Indices 12, 13, and 14 retain their original values:

  • a[12] = 'd'
  • a[13] = '!'
  • a[14] = '\0'

Thus, the array contents starting from index 0 are "Hello World!d!" followed by '\0'. Printing this string yields Hello World!d!.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions