C prototype mismatch and argument promotions: what happens here (Turbo C style)? #include<stdio.h> int main() { extern int fun(float); // prototype says: takes float, returns int int a; a = fun(3.14); // call with float literal printf("%d ", a); return 0; } int fun(int aa) // actual definition: takes int, returns int { return (int)++aa; }

Difficulty: Medium

Correct Answer: Compile Error

Explanation:


Introduction / Context:
Function prototypes in C establish both return type and parameter types for type checking. A mismatch between the prototype and the actual definition leads to diagnostics because the compiler and linker can no longer agree on how arguments are passed or how the function should be called.



Given Data / Assumptions:

  • Prototype: extern int fun(float);
  • Call site: fun(3.14);
  • Definition: int fun(int aa) { return (int)++aa; }
  • Platform: Turbo C (16-bit) still performs type checking with prototypes.


Concept / Approach:
With a prototype in scope, the compiler enforces argument type compatibility. Here, the prototype declares a single float parameter, but the definition provides a single int parameter—this is a type conflict. The compiler must emit a diagnostic (conflicting types/argument mismatch). Older K&R-style, no-prototype calls might allow implicit int promotions, but once a conflicting prototype is present, it is an error.



Step-by-Step Solution:
Compiler reads prototype: expects float.Compiler sees definition: parameter is int.Types differ → conflicting types for “fun”.Compilation fails; no well-defined output exists.



Verification / Alternative check:
Fix either side to match (both as int or both as float). Then, with fun(int) and argument 3.14, the literal would convert to int (likely 3), pre-increment to 4, and print 4—if types matched.



Why Other Options Are Wrong:
A/D reflect a hypothetical fixed program, not the mismatched one. B is impossible since printf("%d") prints integers. C is not mandated. The only correct outcome for the given code is a compile-time error.



Common Pitfalls:
Relying on implicit int or no-prototype calls; forgetting that mismatched prototypes and definitions must be identical in parameter types and return types.



Final Answer:
Compile Error

More Questions from Declarations and Initializations

Discussion & Comments

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