What is the output of the following program? void main() { printf("1"); goto…
2026
What is the output of the following program?
void main()
{
printf("1");
goto xyz;
printf("2");
xyz:
printf("3");
}
Answer: D. 13 — In C, the statement goto label; performs an unconditional jump to a point marked label: within the same function. A label must be followed by a colon, not a…
- A.
1 2
- B.
23
- C.
1 2 3
- D.
13
Attempted by 216 students.
Show answer & explanation
Correct answer: D
In C, the statement goto label; performs an unconditional jump to a point marked label: within the same function. A label must be followed by a colon, not a semicolon — label: marks a jump target, while label; would instead be read as an ordinary (and here, undeclared) expression statement. The instant execution reaches a goto, control moves straight to its target label; every statement written between the goto and the label is skipped, and normal top-to-bottom execution resumes from the label onward.
Control enters
mainand executes the first statement,printf("1"), so1is printed.The next statement is
goto xyz;, an unconditional jump to the labelxyz:. Control transfers there immediately, so the interveningprintf("2")never runs.Execution resumes at
xyz:and continues to the next statement,printf("3"), so3is printed.No further statements remain, so the characters actually printed, in the order they were printed, form the complete output.
If the
gotoline were removed, execution would fall straight through every statement in source order, printing all three characters — confirming that it is specifically the jump, not some other effect, that removes the middle print from the output.The label
xyz:itself performs no printing; it only marks where execution resumes after the jump, so it contributes nothing extra to the output.
The two prints that actually execute, in order, produce the output 13.