GATE CS 2025 Set 2 — Question 19
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+1 / -0.33MediumPointers & Pointer ArithmeticC ProgrammingProgramming & Data StructuresArrays & Strings in C
Programming & Data Structures → C Programming → Arrays & Strings in C
Last updated
Question
Consider the following C program:Which ONE of the following will be the output of the 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++;
}
Correct answer
(D) Hello World!d!
Solution
The program defines a function
stringcopy that copies characters from the source pointer t to the destination pointer s as long as *t is not the null terminator (\0).Inside main:1.
char a[30] is initialized to "@#Hello World!".- The array contents (indices 0-14) are:
@,#,H,e,l,l,o,,W,o,r,l,d,!,\0.
stringcopy(a, a + 2) is called.spoints toa[0]('@').tpoints toa[2]('H').
while(*t) loop executes:- It copies characters from
ttosand increments both pointers. - It copies the substring
"Hello World!"(length 12) from indices 2-13 to indices 0-11. a[0]becomes 'H',a[1]becomes 'e', ...,a[11]becomes '!'.
while(*t) checks the value before copying. When t reaches index 14 (the null terminator \0), the condition fails, and the loop terminates. The assignment *s++ = *t++ is NOT executed for the null terminator.5.Therefore, the null terminator is NOT copied to the new position (index 12). The character at index 12 remains what it was originally.
- Original
a[12]was 'd'. - Original
a[13]was '!'. - Original
a[14]was\0.
a is:- Indices 0-11:
Hello World! - Index 12:
d(unchanged) - Index 13:
!(unchanged) - Index 14:
\0(unchanged) - Result:
"Hello World!d!"
printf("%s\n", a) prints the string up to the first null terminator.- Output:
Hello World!d!
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]