C Compilation Error: implicit declaration of function xxx is invalid in C99 [-Wimplicit-function-declaration]

C compilation error: implicit declaration of function XXX is invalid in C99 [- wimplicit function declaration]

Code file test. C , the content is as follows:

#include <stdio.h>

int main()
{
    printf("Hello, World! \n"); 

    int result = 0;
    result = sum(1 , 5);

    printf("result = %d \n", result);
 
    return 0;
}

int sum(int a, int b) 
{
    printf (" a = %d \n",  a);
    printf (" b = %d \n",  b);
 
    return a + b;
}

The current version of clang is as follows:

$ clang --version
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target:x86_64-apple-darwin19.0.0
Thread model:posix
InstalledDir:../XcodeDefault.xctoolchain/usr/bin

The current version of GCC is as follows:

$ gcc --version
Configured with: 
--prefix=/Applications/Xcode.app/Contents/Developer/usr 
--with-gxx-include-dir=../Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: ../XcodeDefault.xctoolchain/usr/bin

Error in compiling

$ clang test.c
test.c:19:14: warning: implicit declaration of function 'sum' is invalid in C99 
  [-Wimplicit-function-declaration]
    result = sum(1 , 5);
             ^
1 warning generated.
Hello, World! 
 a = 1 
 b = 5 
result = 6

Error: implicit declaration of function ‘sum’ is invalid in C99

The causes are as follows

C language is a procedural programming language, and the order of program execution is from top to bottom. Function calls need to be declared before calling. C99 does not allow implicit declaration by default (the C language standard introduced in 1999). In previous versions, when C language functions were not declared before calling, the compiler would automatically generate assembly code for the C code calling the function according to an implicit declaration rule

Solution:

Declare the main function before calling it

(1) Put it directly in front of the main function
(2) or defined in the. H header file, the header file is introduced before the main function
(3) compile with the old version【 [not recommended]

Use the - STD parameter to specify the C language version:

If you are compiling with clang:

# Use C89 <--no error reported
$ clang test.c -std=c89

# Use C99 <-- Prompt for implicit declaration, error
$ clang test.c -std=c99

If compiling with GCC:

# Use C89 <-- No error
$ gcc test.c -std=c89  

# Use C99 <-- Prompt for implicit declaration, error
$ gcc test.c -std=c99

Full code:

#include <stdio.h>

// function declaration 
int sum (int a, int b); // <---- declared before main function

// Main function entry

int main()
{
    printf("Hello, World! \n"); 
    int result = 0;
    result = sum(1 , 5);

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

    return 0;
}

int sum(int a, int b)
{
    printf (" a = %d \n",  a);
    printf (" b = %d \n",  b);
 
    return a + b;
}

Validation: compilation execution

Compile using clang to execute:

$ clang test.c && ./a.out
Hello, World! 
 a = 1 
 b = 5 
result = 6

Compile with GCC

$ gcc -o test1 test.c 
$ ./test1 
Hello, World! 
 a = 1 
 b = 5 
result = 6

https://stackoverflow.com/questions/13870227/implicit-declaration-of-function-sum-is-invalid-in-c99 https://blog.csdn.net/passerby_unnamed/article/details/51073296

Similar Posts: