Header file in C
In C, a file with .h extension is called Header file. Header file contains function prototype(declarations) and macro definition. These header files can be included in to the other c source files to use its predefined functions or macros.
Advantages of Header file
- Header files provide different type of functionality to perform various operations in c program, Hence reduces development time.
- It can be used in multiple c program files, which avoids code duplicacy.
How to use/include header file
To use header file in c program, #include directive is used. All header files, should be declared at the top of c program. #include directive is used to import/include a header file into a source file.
Syntax
#include<header_file_name.h>
#include"header_file_name.h"
Example
#include<stdio.h>
#include"conio.h"
int main()
{
return 0;
}
Header files can be included in two ways.
<header_file_name.h> | within angle brackets |
"header_file_name.h" | within double quotes |
<header_file_name.h> within angle brackets searches header files in system directories.
"header_file_name.h" within double quotes searches header files in current directory and then system directory.
Header files are of two types
System header files | These files comes with compiler and exposes OS interfaces like IO, memory, storage etc. |
User header files | A header file created by a programmer for their projects or software's to make code cleaner, easier to debug and no code duplicacy. |