In C programming, what will be the output of the following program that passes a float argument to an external function and returns it as an int? main() { extern int fun(float); int a; a = fun(3.14); printf("%d", a); } int fun(float aa) { return ( (int) aa ); }

Difficulty: Medium

Correct Answer: 3

Explanation:


Introduction / Context:
This question tests your understanding of how C handles type casting from float to int, how function declarations with extern work and how printf displays integer values. Being comfortable with implicit and explicit conversions is critical when reading or debugging C code that mixes different numeric types.


Given Data / Assumptions:

    • The function fun takes a float parameter and returns an int value.

    • The call is a = fun(3.14); inside main.

    • The function fun returns (int) aa, an explicit cast from float to int.

    • printf is called with the format string "%d" and the argument a.

    • We assume the program is compiled as valid C with the proper stdio.h include added.



Concept / Approach:
The key idea is that casting a floating point value to an int in C truncates the fractional part towards zero. The extern keyword simply informs the compiler that fun is defined elsewhere; it does not change runtime behavior. Once the truncated integer is returned, printf("%d", a) prints that integer in decimal format. So we only need to track the conversion 3.14 → 3 and then the straightforward integer print.


Step-by-Step Solution:
Step 1: The literal 3.14 is of type double by default. When passed to fun, it is converted to float as declared in the parameter list. Step 2: Inside fun, the parameter aa holds approximately 3.14 in float precision. Step 3: The return statement casts aa to int: (int) aa. In C, casting a positive float to int drops the fractional part, so (int) 3.14 becomes 3. Step 4: The returned value 3 is assigned to the integer variable a in main. Step 5: printf("%d", a); prints the integer value stored in a, which is 3, producing the output 3 with no newline or spaces.


Verification / Alternative check:
You can quickly verify by trying similar casts in a small test program, for example casting 2.99, 5.0 and 0.75 to int. In all cases, C truncates towards zero, giving 2, 5 and 0 respectively. This is consistent with 3.14 becoming 3. The extern declaration is also standard: it simply tells the compiler that fun has external linkage and will be linked in, which is satisfied by the later definition in the same translation unit.


Why Other Options Are Wrong:
Option B (3.14) is wrong because the function does not return a float; it explicitly casts to int, removing the fractional part. Option C (0) is wrong since 3.14 is not close to zero and truncation does not round down to the nearest integer; it simply drops the decimals. Option D is wrong because there is no error solely due to the extern keyword; the declaration and definition match in parameter and return types, so the code is valid when stdio.h is included.


Common Pitfalls:
A common mistake is to assume that casting a float to int rounds to the nearest whole number; in C it truncates toward zero instead. Another pitfall is to overthink the extern keyword and assume it changes runtime semantics. It is only a linkage specifier, not a storage or conversion rule. Also, forgetting to match printf format specifiers and argument types can cause undefined behavior, but here "%d" is correctly paired with an int. Carefully reading both the function signature and the cast in the return statement avoids these errors.


Final Answer:
The program prints the integer part of 3.14, so the output is 3.

More Questions from Programming

Discussion & Comments

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