Programming languages such as C and C++ have header files and Library files. These languages keep constants and function prototypes in header files. A programmer can write header file on his own or they come with the compiler. Header files are useful as they make the program more organized and manageable. If all the defined functions are in the same file, it makes the program complex. Therefore, the programmer can include the required header file when writing the program. A header file consists of the function declarations. These declarations tell the compiler about function name, return type and parameters. Library file contains the actual implementation of the function declared the in header file. C library and C++ library are library files. Therefore, the key difference between header file and library file is that header file contains the function declarations to be shared between several source files while library file is a file that contains the function definition for the declared functions in the header file.
CONTENTS
1. Overview and Key Difference
2. What is Header File
3. What is Library File
4. Similarities Between Header File and Library File
5. Side by Side Comparison – Header File vs Library File in Tabular Form
6. Summary
What is Header File?
A Header file contains the function declarations. The programmer can write the header file or it comes along with the compiler. A declaration tells the compiler about the function name, return type and parameters. In C language, header files have .h extension. The header files are included in the C program using preprocessor directive. The syntax of adding header file in C by #include <file>. If the programmer wants to include the math header file, can write the statement #include <math.h>.
The <stdio.h> header file contains the functions defined for input and output. The fclose is used to close the stream. The printf is used to send a formatted output to standard output. The fscanf is used to read a formatted input from standard input. The <conio.h> header file contains the functions related to the console. The getch is used to read a character from the console. The header file <string.h> contains the functions relevant to string manipulation. The strlen is to find the length of the string. The function strcmp is to compare two strings.
The functions necessary for graphics programming is included in the <graphics.h> header file. The <math.h> header file contains the mathematics related operations. The rand is used to create a random number. The pow function is used to find the power of a number. Some other math functions are sin, cos, tan, sqrt. These functions are already declared in the header files.
Including header files in C++ is also similar to C. That is also using preprocessor directives. The syntax of adding header file in C++ is #include <file>. If the programmer wants to include the iostream header file, it is done using #include <iostream.h>. It is the standard input-output streams library. The cin is standard input stream. The cout is for the standard output stream.
Including a header file is similar to copying and pasting the content of the header file. It can cause errors and can be a complex process if there are many source files. Likewise, the header files can be included in the programs.
What is Library File?
A library file will have the function definitions for the declared functions in the header file. Function definitions are the actual implementation of the function. The programmer uses the functions declared in the header files in the program. It is not necessary to implement them from the beginning. When compiling the program, the compiler finds the definitions in library file for the declared functions in the header file.
Even though the header files are included in the program by the programmer, the related library files are found by the compiler automatically. Therefore, the compiler uses the library files to find the actual implementations of the declared functions in the header files. If printf() function is used in the program, the definition for how it works is in the related library file. If math.h is the header file, math.lib is the library file.
What is the Similarity Between Header File and Library File?
- Both these are used in C/C++ language.
What is the Difference Between Header File and Library File?
Header File vs Library File |
|
Header file is a file that contains the function declarations to be shared between several source files. | Library file is a file that contains the function definition for the declared functions in the header file. |
Format | |
Header file has a text format. | Library file has a binary format. |
Including Method | |
The programmer includes the header files. | The compiler relates the relevant library files automatically to the program. |
Modification | |
Header file can be modified. | Library file cannot be modified. |
Summary – Header File vs Library File
Header file and library file is associated with programming languages such as C and C++. This article discusses the difference between a header file and library file. The difference between a header file and library file is that header file contains the function declarations to be shared between several source files while library file is a file that contains the function definition for the declared functions in the header file. Header files contain the prototypes and calls of the functions. It does not include the functionalities of the functions. A header file is a gateway to the library file that contains the real functionality.
Download the PDF Version of Header File vs Library File
You can download PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Header File and Library File
Reference:
1.nareshtechnologies. Library and IDE | C Language Tutorial, Naresh i Technologies, 27 Aug. 2016. Available here
2.tutorialspoint.com. “C Header Files.” The Point. Available here
3.tutorialspoint.com. “C Library .” The Point. Available here