Skip to main content

Implicit Declaration of Function in C Pratik Sah The Crazy Programmer

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.



from The Crazy Programmer https://ift.tt/3h7NISW

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?...

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet...

Olive and NTT DATA Join Forces to Accelerate the Global Development and Deployment of AI Solutions

U.S.A., March 14, 2021 — Olive , the automation company creating the Internet of Healthcare, today announced an alliance with NTT DATA , a global digital business and IT services leader. The collaboration will fast track the creation of new healthcare solutions to transform the health experience for humans — both in the traditional healthcare setting and at home. As a member of Olive’s Deploy, Develop and Distribute Partnership Programs , NTT DATA is leveraging Olive’s open platform to innovate, build and distribute solutions to Olive’s customers, which include some of the country’s largest health providers. Olive and NTT DATA will co-develop new Loops — applications that work on Olive’s platform to provide humans real-time intelligence — and new machine learning and robotic process automation (RPA) models. NTT DATA and Olive will devote an early focus to enabling efficiencies in supply chain and IT, with other disciplines to follow. “This is an exciting period of growth at Olive, so...