Consider the following C program that uses a goto inside a for loop: #include <stdio.h> int main() { int i = 0, k; if (i == 0) goto label; for (k = 0; k < 3; k++) { printf("hi "); label: k = printf("%03d", i); } return 0; } What does this program print on the screen?

Difficulty: Hard

Correct Answer: It prints 000 exactly once

Explanation:


Introduction / Context:
This question tests your understanding of control flow in C, particularly how goto interacts with for loops and how assignments inside the loop body can change the loop index. It also highlights that printf returns the number of characters printed, which can affect later loop iterations when assigned to the loop variable.


Given Data / Assumptions:

    The variable i is initialized to 0 and never changed.
    Because i is 0, the condition if (i == 0) is true, so goto label jumps directly into the body of the for loop at the label line.
    The for loop is declared as for (k = 0; k < 3; k++), but k is not initialized before the goto jumps into the loop body.
    Inside the loop, the statement k = printf("%03d", i); both prints a formatted number and assigns k the number of characters printed.


Concept / Approach:
When the goto label is executed before the for loop, control is transferred to the label inside the loop body. In C, the for loop still has its increment and condition checks executed after each iteration, regardless of where the jump enters. The key trick here is that k is set equal to the return value of printf, which for the format "%03d" and value 0 will print three characters "000" and return 3. This large value for k causes the loop to terminate on the next condition check.


Step-by-Step Solution:
Program starts with i = 0. The condition if (i == 0) is true, so it executes goto label. Control jumps to the label inside the for loop, before any normal iteration of the loop has started. At this point k has not yet been assigned by the for initialization, but the next statement executed is k = printf("%03d", i);. printf("%03d", 0) prints the three character string 000 and returns 3, so k becomes 3. No "hi" line is printed because the printf("hi") call is above the label and is skipped due to the goto. At the end of the loop body, control reaches the k++ part of the for statement. Since k is 3, k++ makes it 4. The loop condition is then checked: k < 3 is now 4 < 3, which is false, so the loop terminates immediately.


Verification / Alternative check:
If you instrument the code with additional prints showing k at each step, you will see k being assigned 3, then incremented to 4, and the loop ending after the first iteration. Only the "000" output from the single printf is visible on the screen, confirming that this is the only output.


Why Other Options Are Wrong:
Option b suggests that "hi" is printed three times followed by multiple zeros, but the goto jumps directly to the label, skipping the hi printing statement entirely.
Option c describes a more complex mixture of outputs that would require several loop iterations, which do not occur because k is set to a value that terminates the loop quickly.
Option d claims no output, but the call to printf inside the loop body is executed once and prints 000.


Common Pitfalls:
Many learners assume that a for loop cannot be entered in the middle, but goto can jump into the loop body, which can be very confusing and unsafe. Another pitfall is forgetting that printf returns the number of characters printed and that assigning this to the loop variable can drastically change the loop behavior. It is generally best to avoid goto in such contexts in real code.


Final Answer:
The program executes the printf at the label exactly once and then exits the loop, so it prints 000 a single time.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion