• Home
  • 6.14 Implicit Function Declarations

6.14 Implicit Function Declarations

View Categories

6.14 Implicit Function Declarations

An implicit function declaration occurs when a function is called without a prior declaration that specifies its interface. Older C standards permitted certain implicit declarations, but C99 and later do not allow implicit function declarations.

#include <stdio.h>

int main(void)
{
    int result = calculate(10, 20);

    printf("Result = %d\n", result);

    return 0;
}

int calculate(int a, int b)
{
    return a + b;
}

Example Output #

The program is not valid C99-or-later code as written. A conforming implementation must diagnose the undeclared call to calculate().

Explanation #

The function call appears before the compiler has seen a declaration:

int result = calculate(10, 20);

The definition appears later:

int calculate(int a, int b)
{
    return a + b;
}

A later definition does not retroactively provide a declaration for an earlier call.

Corrected Version #

Declare the function before calling it:

#include <stdio.h>

int calculate(int a, int b);

int main(void)
{
    int result = calculate(10, 20);

    printf("Result = %d\n", result);

    return 0;
}

int calculate(int a, int b)
{
    return a + b;
}

Example Output #

Result = 30

C Standard Behavior #

Standard Implicit function declarations
C90 Permitted in certain cases, with an assumed return type of int
C99 Removed
C11 Not permitted
C17 Not permitted
C23 Not permitted

Implicit declarations were unsafe because the assumed function interface could differ from the actual definition, potentially causing incorrect behavior. Modern C code should declare functions before calling them.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *