Modern ISO C (C99/C11+): will the following functions compile and work without a prior prototype for f2? int f1(int a, int b) { return ( f2(20) ); } int f2(int a) { return (a*a); } Assume no forward declaration is provided.
-
AYes
-
BNo
-
COnly if the compiler allows implicit int and implicit declarations
-
DWorks in C++ but never in C
-
EOnly with optimization level -O3
Answer
Correct Answer: No
Explanation
Introduction / Context:Earlier C dialects (pre-C99) allowed implicit function declarations, which made calling a function before its prototype compile with at most a warning. Modern ISO C removed implicit int and implicit function declarations. This question examines whether a call to f2 before any visible declaration compiles cleanly today.
Given Data / Assumptions:
- Language mode: ISO C99/C11 or later.
- No forward declaration of f2 is present before f1.
- Typical compilers (GCC/Clang/MSVC) with standards-compliance diagnostics.
Concept / Approach:In modern C, a function must be declared before it is used so that the compiler can check the parameter types and return type. Calling f2 in f1 without a prior visible prototype violates this rule and is diagnosed as an error in strict standard modes. Adding a declaration like int f2(int); before f1 resolves the issue.
Step-by-Step Solution:
Inspect f1: it calls f2(20) without any visible prototype.Modern C forbids implicit declarations; this triggers a compile-time error.Provide a forward declaration or reorder definitions so f2 appears before f1.With a prototype, both functions compile and run; f1 returns 400.Verification / Alternative check:Compile with -std=c11 -Wall -Werror. Without a prototype, the build fails; adding int f2(int); before f1 succeeds.
Why Other Options Are Wrong:
Yes: Not correct for modern standard modes.Only if the compiler allows implicit declarations: that describes non-standard extensions or very old modes, not modern ISO C.Works in C++ but never in C: C++ also requires declarations before use; so this justification is wrong.Only with -O3: Optimization does not change the need for a prototype.Common Pitfalls:Assuming “it works on my old Turbo C” implies it is valid today; relying on implicit int or implicit declarations is non-portable and rejected by modern compilers.
Final Answer:No.