Alex_ban
Alex_ban

Reputation: 321

Header Files and Libraries

Do I really need header files for using libraries?My code works without header files and the libraries are linked perfectly except that I get a "warning: implicit declaration of function" message.Still its just a warning my program runs.

Upvotes: 1

Views: 140

Answers (4)

user529758
user529758

Reputation:

Do I really need header files for using libraries?

Yes.

My code works without header files

No, it doesn't. It only appears to be working.

and the libraries are linked perfectly except that I get a "warning: implicit declaration of function" message

In this case, I wouldn't call it "perfect".

Still its just a warning my program runs.

And you can never know what it does since it now invokes undefined behavior. "Just a warning"? Sure. There's a reason the compiler warns you. Respect warnings and fix them.

Here's the quote from C99 (6.5.2.2.6) which explains why the program has UB without headers (more precisely, without proper function prototypes which you don't seem to have either):

  1. If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined.

If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined.

If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined, except for the following cases:

— one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types;

— both types are pointers to qualified or unqualified versions of a character type or void.

There's a very good reason header files are used. People who invented and designed the language knew what they were doing, and they made this decision because it's a necessary thing and a good solution to the problem that types need to be visible across compilation units.

Upvotes: 1

Sakamoto
Sakamoto

Reputation: 155

My code works without header files and the libraries are linked perfectly except that I get a "warning: implicit declaration of function" message.Still its just a warning my program runs.

It is a bad practice to ignore a warning from the compiler. Programs that are not compiled properly could have bugs that might cause you bigger problems and errors in the future.

So for example, if you get a warning:

prog.c:12:5: warning: implicit declaration of /*some function used in the program*/

It is expected that you must include the header file wherein that function is defined.

Upvotes: 1

Gaurav
Gaurav

Reputation: 2103

For most of the starters this warning is seen when they start using

malloc  

So I am using this example. For the warning in this case You need to add:

#include <stdlib.h>

This file includes the declaration for the built-in function malloc. If you don't do that, the compiler thinks you want to define your own function named malloc and it warns you because:

  1. You don't explicitly declare it and
  2. There already is a built-in function by that name which has a different signature than the one that was implicitly declared (when a function is declared implicitly, its return and argument types are assumed to be int, which isn't compatible with the built-in malloc, which takes a size_t and returns a void*).

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172588

Header file:- is a file that allows programmers to separate certain elements of a program's source code into reusable files.

A library file is the executable code that works according to what is specified in the header file.

I get a "warning: implicit declaration of function" message.

There may be many reasons for this warning like maybe your header guard is not working right. Another possibility is that you have declared the function Func but called it with func.

Check this out:-

Including a header file is equal to copying the content of the header file but we do not do it because it will be very much error-prone and it is not a good idea to copy the content of header file in the source files, specially if we have multiple source file comprising our program.

Upvotes: 1

Related Questions