Translate

Wednesday 10 June 2020

Implicit Declaration of Function in C

We are very much familiar with the flow control in C where it follows the Top-Down approach, and when we are writing a C program and if we are using any function, we might have come across a very common error ‘Implicit declaration of function’.

Now why this error occurred? The answer is already in the error.

We have used a function in our program which is not declared yet or we can say that we have used a function implicitly.

Implicit declaration of the function is not allowed in C programming. Every function must be explicitly declared before it can be called.

In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration.

Here is a small code that will give us an Implicit declaration of function error.

#include <stdio.h>

int main(void) {
  int a = 10;
  int b = 20;
  printf("The value of %d + %d is %d",a, b, addTwo(10, 20));
  return 0;
}

Now the above code will give you an error of Implicit declaration.

clang-7 -pthread -lm -o main main.c
main.c:6:45: warning: implicit declaration of function
      'addTwo' is invalid in C99
      [-Wimplicit-function-declaration]
  printf("The value of %d + %d is %d",a, b, addTwo(10...
                                            ^
1 warning generated.
/tmp/main-c6e933.o: In function `main':
main.c:(.text+0x38): undefined reference to `addTwo'
clang-7compiler exit status 1
: error: linker command failed with exit code 1 (use -v to see invocation)

Here are some of the reasons why this is giving error.

  1. Using a function that is pre-defined but you forget to include the header file for that function.
  2. If you are using a function that you have created but you failed to declare it in the code. It’s better to declare the function before the main.

In C90, this error can be removed just by declaring your function before the main function.

For example:

#include <stdio.h>
int yourFunctionName(int firstArg, int secondArg);

int main() {
// your code here
// your function call
}

int yourFunctionName(int firstArg, int secondArg) {
// body of the function
}

In the case of C99, you can skip the declaration but it will give us a small warning and it can be ignored but the definition of the function is important.

#include <stdio.h>
// optional declaration
int main(void) {
  int a = 10;
  int b = 20;
  printf("The value of %d + %d is %d",a, b, addTwo(10, 20));
  return 0;
}

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

This gives us the output:

clang-7 -pthread -lm -o main main.c
main.c:6:45: warning: implicit declaration of function
      'addTwo' is invalid in C99
      [-Wimplicit-function-declaration]
  printf("The value of %d + %d is %d",a, b, addTwo(10...
                                            ^
1 warning generated.
./main
The value of 10 + 20 is 30

Here you can see that our code is working fine and a warning is generated but we are good to go but this is not recommended.

Well, this was all about the Implicit declaration of the function in C and the error related to it. If you stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug on your own as someone might have faced a similar problem earlier.

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.

Thanks for your visit and if you are new here, consider subscribing to our newsletter. See you in my next post. Bye!

The post Implicit Declaration of Function in C appeared first on The Crazy Programmer.



No comments:

Post a Comment