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;
}

Assume the program is compiled in ISO C17 mode with GCC 14 or later, or Clang 16 or later, using default diagnostic severity. If Line 1 is deleted, which result best describes the compilation?

Answer: D. compilation fails with compiler errorsConceptA C function call is checked against a declaration visible before the call. C99 and later do not permit implicit function declarations and require a…

  1. A.

    no compile warning or error

  2. B.

    some compiler warnings not leading to unintended results

  3. C.

    some compiler warnings due to a type mismatch, eventually leading to unintended results

  4. D.

    compilation fails with compiler errors

Attempted by 285 students.

Show answer & explanation

Correct answer: D

Concept

A C function call is checked against a declaration visible before the call. C99 and later do not permit implicit function declarations and require a diagnostic for an undeclared call.

The C standard requires a diagnostic but leaves warning-versus-error severity to the implementation, so a precise compiler mode is needed to determine whether translation continues.

Application

  1. Deleting Line 1 removes the only declaration of foo that is visible before the call inside main.

  2. In the stated ISO C17 modes, GCC 14 or later and Clang 16 or later diagnose this undeclared call as an error by default.

  3. Even on a permissive legacy path that assumes foo returns int, the later definition returning double conflicts with that assumed function type.

  4. The stated compilation therefore fails before the program can reach runtime.

Cross-check and contrast

  • A no-diagnostic outcome requires a permissive mode outside the stated compiler configuration.

  • A harmless-warning outcome does not fit the default error severity in the stated compiler modes.

  • A warning followed by unintended runtime behavior requires compilation to continue after the type mismatch, which the stated modes do not do.

Therefore, deleting Line 1 makes the compilation fail with compiler errors.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…