Consider the following C-program: double foo (double); /* Line 1 */ int main()…
2005
Consider the following C-program:
double foo (double); /* Line 1 */
int main()
{
double da, db;
// input da
db = foo(da);
}
double foo(double a)
{
return a;
}
The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:
- A.
no compile warning or error
- B.
some compiler-warnings not leading to unintended results
- C.
some compiler-warnings due to type-mismatch eventually leading to unintended results
- D.
compiler errors
Attempted by 200 students.
Show answer & explanation
Correct answer: D
Answer: compiler errors.
Explanation: In C99 and later, implicit function declarations were removed. Calling a function that has not been previously declared is a violation of the standard and requires a diagnostic; most modern compilers treat this as a compilation error.
Standards behavior: Implicit function declarations were allowed in very old C (pre-C99). Since C99, code must declare a function before use.
Old compilers: When no prototype was present, the compiler assumed the function returned int and performed default argument promotions; this could generate warnings and cause incorrect runtime behavior if the real signature differs (for example, returning double).
Modern compilers: The absence of a prior declaration is diagnosed; most toolchains will emit an error under current language standards.
Fix: Keep the function prototype before main or place the function definition before main.
Example fix (keep prototype):
double foo(double); /* prototype */
int main() {
double da, db;
// input da
db = foo(da);
}
double foo(double a) { return a; }
Summary: The original solution was missing. This improved solution explains the modern standard behavior, contrasts historical behavior, and gives concrete fixes so learners understand why removing the prototype leads to a compile-time error.