Question Details

#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++;

}

Options

A

@#Hello World!

B

Hello World!

C

ello World!

D

Hello WorldId!

Show Answer
Correct Answer
D Hello WorldId!

Solution :

The correct option is Hello WorldId!.

Let us trace the execution of the C program step-by-step to understand how this output is generated.

1. Initialization of the character array:
The array a is defined in the main function as:
char a[30] = "@#Hello World!";
This initializes a with the string "@#Hello World!", which contains 14 characters plus a terminating null character ('\0').
The memory layout of the array a initially is:
a[0] = '@'
a[1] = '#'
a[2] = 'H'
a[3] = 'e'
a[4] = 'l'
a[5] = 'l'
a[6] = 'o'
a[7] = ' '
a[8] = 'W'
a[9] = 'o'
a[10] = 'r'
a[11] = 'l'
a[12] = 'd'
a[13] = '!'
a[14] = '\0'

2. Function Call:
The program calls stringcopy(a, a + 2).
Here, the destination pointer s points to a (i.e., a[0], which is '@').
The source pointer t points to a + 2 (i.e., a[2], which is 'H').

3. Overlapping Memory & Copy Loop:
Inside stringcopy(char *s, char *t):
while(*t)
    *s++ = *t++;
This loop copies the characters from the source pointer t to the destination pointer s one-by-one, incrementing both pointers after each assignment, until the null terminator at *t is encountered.
Let's trace the loop iterations:
• Iteration 1: *t is 'H'. Since it is non-zero, *s (which is a[0]) becomes 'H'. s now points to a[1], and t points to a[3].
• Iteration 2: *t is 'e'. a[1] becomes 'e'. s becomes a[2], t becomes a[4].
• Iteration 3: *t is 'l'. a[2] becomes 'l'. s becomes a[3], t becomes a[5].
• Iteration 4: *t is 'l'. a[3] becomes 'l'. s becomes a[4], t becomes a[6].
• Iteration 5: *t is 'o'. a[4] becomes 'o'. s becomes a[5], t becomes a[7].
• Iteration 6: *t is ' ' (space). a[5] becomes ' '. s becomes a[6], t becomes a[8].
• Iteration 7: *t is 'W'. a[6] becomes 'W'. s becomes a[7], t becomes a[9].
• Iteration 8: *t is 'o'. a[7] becomes 'o'. s becomes a[8], t becomes a[10].
• Iteration 9: *t is 'r'. a[8] becomes 'r'. s becomes a[9], t becomes a[11].
• Iteration 10: *t is 'l'. a[9] becomes 'l'. s becomes a[10], t becomes a[12].
• Iteration 11: *t is 'd'. a[10] becomes 'd'. s becomes a[11], t becomes a[13].
• Iteration 12: *t is '!'. a[11] becomes '!'. s becomes a[12], t becomes a[14].
• Iteration 13: *t (which is a[14]) is '\0'. The loop condition while(*t) evaluates to false, and the loop terminates.

4. Final state of the array:
Crucially, notice that the null character at the end of the source string was never copied because the loop terminated as soon as it saw *t == '\0'.
Let us look at the state of array a after the function returns:
a[0] = 'H' (overwritten)
a[1] = 'e' (overwritten)
a[2] = 'l' (overwritten)
a[3] = 'l' (overwritten)
a[4] = 'o' (overwritten)
a[5] = ' ' (overwritten)
a[6] = 'W' (overwritten)
a[7] = 'o' (overwritten)
a[8] = 'r' (overwritten)
a[9] = 'l' (overwritten)
a[10] = 'd' (overwritten)
a[11] = '!' (overwritten)
a[12] = 'd' (unchanged from initial initialization)
a[13] = '!' (unchanged from initial initialization)
a[14] = '\0' (unchanged from initial initialization)

When printf("%s\n", a); is called, it prints characters from index 0 until it hits the first null character ('\0') at index 14.
This outputs: Hello WorldId!

Transparent Access on Mindyard

Built for Learners & Educators Alike

Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.

FREE FOR STUDENTS

Practice & Learn Free for Students

Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.

  • Unlimited mock tests & question bank practice
  • Full access to curated study library & formula notes
  • 100% free for students • No credit card required
2 MONTHS FREE TRIAL

Educators & Coaching Institutes

Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.

  • AI question uploader & rapid test generator
  • Anti-cheating tab proctoring & performance reports
  • Full Pro access free for 60 days • No upfront commitment
Discover more resources

You may also like

Mock Tests

View All
  • GATE
  • beginner
  • 3 hours
  • civil engineering

Prepare for GATE 2026 Civil Engineering with the latest question papers, previous year papers, detailed solutions, syllabus, exam pattern, preparation tips, mock tests, and subject-wise study material. Download GATE 2026 CE question papers with solutions in PDF format and boost your preparation for top engineering institutes and PSU recruitment.

  • GATE
  • beginner
  • 3 hours
  • electronics and communication engineering

Prepare effectively for GATE 2026 Electronics & Communication Engineering (EC) with this comprehensive Question Paper with Solutions. This resource includes detailed explanations, concept-wise solutions, previous year paper analysis, and exam-oriented practice to help aspirants strengthen core subjects like Electronic Devices, Analog Circuits, Digital Circuits, Signals & Systems, Network Theory, Control Systems, Electromagnetics, Communications, and more. Perfect for revision, self-assessment, and boosting your GATE 2026 EC preparation strategy.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...
Ask your own doubt or choose a quick prompt: