Difference between Header file and Library


In C, C#, Java or any other programming language, both header files and libraries provide some specific APIs and their functionality to be used in the main file. Both the header file and the library are combined and used as a single unit. But, on the basis of nature of files, we can distinguish between a Header File and a Library.

Read this tutorial to learn more about header files and libraries and how they are different from each other.

What is a Header File?

The file that contains all the header names which are to be used in the main code file is referred to as a header file. A header file has the extension of ".h". A header file has only header names, therefore it contains only the function declarations. Header files are responsible for instructing the complier how to call the functionalities.

A header file contains the prototypes of functions, data types, and constants that are used with the libraries. In a computer program, the header files are used with the help of "#include".

What is a Library?

The file that contains the body of a function is called a library. In other words, a library is a file where the actual implementation of each header is mentioned. The library file has the extension of ".lib". The library file contains the actual definitions of the functions. There are two types of libraries namely, Static Library and Dynamic Library.

Static libraries are those that contain the object code which are linked with an end-user application. Static libraries are mainly used at compile time. On the other hand, dynamic libraries, also called shared libraries, are those that are required at runtime only. Thus, an end-user can compile the code without using the dynamic libraries.

Difference between a Header File and a Library

The following table highlights all the important differences between header file and library −

Key

Header File

Library

Definition

A Header File is the file where all the headers name are mentioned that going to be used or consumed in the main code file.

A Library is the file where the implementation code of each header is written down which is mentioned in the Header file.

File Extension

Header files have the extension of ".h".

Library files have the extension of ".lib".

File Nature

Header files contains the function declarations only.

A Library file contains the implementation code of the header, i.e., it has the actual function definition which is declared in the Header file.

File Location

Header files are available inside include sub directory which itself is in Turbo compiler.

Library files are available inside lib sub directory which also it is in Turbo compiler.

File Format

As Header file only have header name so are human readable as they are in the form of source code.

Library files have actual implementation code so are non-human readable as they are in the form of machine code.

Conclusion

The most significant difference that you should note here is that a header file is the file where all the header names are mentioned, whereas a library is the file where the implementation code of each header is written.

Updated on: 21-Feb-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements