On a 16-bit Turbo C (DOS) platform using compiler registers, what will this program print?
#include
int main()
{
int fun();
int i;
i = fun();
printf("%d
", i);
return 0;
}
int fun()
{
_AX = 1990; // set return register (Turbo C extension) for int
}
-
AGarbage value
-
B0 (Zero)
-
C1990
-
DNo output
-
EImplementation-defined but non-deterministic
Answer
Correct Answer: 1990
Explanation
Introduction / Context:This question targets legacy 16-bit DOS Turbo C behavior where compiler-provided register variables (such as _AX) can be used to specify a function’s return value directly. It assesses platform-specific calling conventions and how return values are passed.
Given Data / Assumptions:
- Compiler: Turbo C in 16-bit DOS.
- Function prototype: int fun();
- In the definition, _AX = 1990; is used without an explicit return statement.
- On this platform, 16-bit integer return values are delivered in the AX register.
Concept / Approach:Many 16-bit compilers map function return values to specific CPU registers. For an int, Turbo C returns via AX. Writing to _AX (a compiler extension naming the register) before the function exits sets the return value exactly as if the function had executed return 1990;.
Step-by-Step Solution:Call fun().Inside fun, assign _AX = 1990.Function returns; caller receives AX content as the integer result.Prints 1990.
Verification / Alternative check:Replacing _AX = 1990; with return 1990; is portable and yields the same output. The register assignment method is non-portable and specific to Turbo C.
Why Other Options Are Wrong:“Garbage value” or “0” would occur only if the convention were different or the code uninitialized the register, which is not the case here. “No output” is incorrect; printf executes.
Common Pitfalls:Attempting to use _AX on modern compilers where it is undefined; assuming this trick is portable across architectures or compilers.
Final Answer:1990