Skip to main content

GATE CSE 2025Programming in C › Pointers, Arrays & StringsVerified

GATE CSE 2025 PYQ: ​​​​​Consider the following C program: #include <stdio.h> void stringcopy ( char *, char *); int main () {…

​​​​​Consider the following C program:

  1. #include <stdio.h>
  2. void stringcopy (char *, char *);
  3. int main() {
  4. char a[30] = "@#Hello World!";
  5. stringcopy(a, a+2);
  6. printf("%s\n",a);
  7. return 0;
  8. }
  9. void stringcopy(char *s, char *t) {
  10. while (*t)
  11. *s++=*t++;
  12. }

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

  1. A
    @#Hello World!
  2. B
    Hello World!
  3. C
    ello World!
  4. D
    Hello World!d!
    Correct answer

Answer

(D) Hello World!d!

Approach

The call stringcopy(a, a+2) makes the source pointer t trail 2 positions ahead of the destination pointer s inside the SAME array. Every iteration writes a[k+2] into a[k], so the string starting at a[2] slides two places to the left, overwriting '@' and '#'. The loop's stopping test is the ORIGINAL $\texttt{'\textbackslash 0'}$ at the end of the array, which the loop never overwrites (the destination always lags the source, so no data is destroyed before it is read). Consequently the copy stops early and the tail characters that were never rewritten remain in the buffer, and they are printed too.

The copy loop shifts a[2..13] into a[0..11], leaving a[12]='d', a[13]='!', $a[14]=\texttt{'\textbackslash 0'}$ intact, so printf prints "Hello World!d!".

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.