Difficulty: Medium
Correct Answer: Compilation error because the label is in a different function
Explanation:
Introduction / Context:
This question examines your understanding of the rules governing goto statements and labels in C. While goto can be used for jumps within a function, it has strict limitations. One of the most important is that a goto cannot jump to a label that is defined in a different function. Violating this rule leads to a compile time error.
Given Data / Assumptions:
• The label here: is defined inside the function fun.
• The goto here; statement is used inside main.
• Both functions are defined in the same translation unit but are distinct functions.
• The code is otherwise assumed to use correct syntax.
Concept / Approach:
In C, labels used by goto are local to the function in which they are defined. This means a goto inside one function may only target labels within the same function body. Jumping into another function, up the call stack or into a different scope in an illegal way is not allowed. The compiler enforces this by rejecting code where a goto label cannot be resolved in the current function.
Step-by-Step Solution:
Step 1: Identify the label declaration: here: appears inside fun, making it local to fun.
Step 2: Observe the goto statement in main: goto here; appears in a different function, main.
Step 3: The C standard specifies that labels have function scope only and cannot be referenced from another function.
Step 4: When the compiler processes main, it cannot associate the label here with any label declared in main, so it reports an error.
Step 5: Because of this, the program fails to compile and never runs, so it does not produce any runtime output or exceptions.
Verification / Alternative check:
If you move the label here: into main, for example placing it before printf("PP") in a block within main, then goto here; would become valid as long as it does not violate other rules such as jumping into a different block with initialized variables. The fact that this modification makes the program compile confirms that the issue in the original code is the cross function jump, not the use of goto itself.
Why Other Options Are Wrong:
Option A ("1") and Option B ("2") suggest that the program compiles and runs, printing some limited output, which is impossible because compilation fails first. Option D ("Exception occurs at runtime") is also incorrect, as the program never reaches runtime due to the compile time violation of the goto rules. The only correct characterization is that the compiler rejects the code because the label is not in the same function as the goto.
Common Pitfalls:
A common misunderstanding is to think of goto as a general jump mechanism like in assembly, capable of jumping across functions and arbitrary addresses. In C, goto is much more restricted and is only meant for local jumps within a function. Another pitfall is forgetting that labels have function scope, not file scope. Overusing goto can also lead to unstructured code, which is why structured control constructs (if, while, for) are generally preferred, but even when goto is used, it must respect these scoping rules.
Final Answer:
Because the goto attempts to jump to a label in another function, the program does not compile and produces a compilation error.
Discussion & Comments