In C language, header files contain a set of predefined standard library functions. The .h is the extension of the header files in C and we request to use a header file in our program by including it with the C preprocessing directive “#include”.
C Header files offer the features like library functions, data types, macros, etc by importing them into the program with the help of a preprocessor directive “#include”.
Syntax of Header Files in C
We can include header files in C by using one of the given two syntax whether it is a pre-defined or user-defined header file.
#include <filename.h> // for files in system/default directory
or
#include "filename.h" // for files in same directory as source file
The “#include” preprocessor directs the compiler that the header file needs to be processed before compilation and includes all the necessary data types and function definitions.
C Header File
Example of Header File in C
The below example demonstrates the use of header files using standard input and output stdio.h header file
C
#include <stdio.h>
int main()
{
printf (
"Printf() is the function in stdio.h header file" );
return 0;
}
|
Output
Printf() is the function in stdio.h header file
Types of C Header Files
There are two types of header files in C:
- Standard / Pre-existing header files
- Non-standard / User-defined header files
1. Standard Header Files in C and Their Uses
Standard header files contain the libraries defined in the ISO standard of the C programming language. They are stored in the default directory of the compiler and are present in all the C compilers from any vendor.
There are 31 standard header files in the latest version of C language. Following is the list of some commonly used header files in C:
Header File
|
Description
|
<assert.h> |
It contains information for adding diagnostics that aid program debugging. |
<errorno.h> |
It is used to perform error handling operations like errno(), strerror(), perror(), etc. |
<float.h> |
It contains a set of various platform-dependent constants related to floating point values. These constants are proposed by ANSI C.
They make programs more portable. Some examples of constants included in this header file are- e(exponent), b(base/radix), etc.
|
<math.h> |
It is used to perform mathematical operations like sqrt(), log2(), pow(), etc. |
<signal.h> |
It is used to perform signal handling functions like signal() and raise(). |
<stdarg.h> |
It is used to perform standard argument functions like va_start() and va_arg(). It is also used to indicate start of the
variable-length argument list and to fetch the arguments from the variable-length argument list in the program respectively.
|
<ctype.h> |
It contains function prototypes for functions that test characters for certain properties, and also function prototypes for
functions that can be used to convert uppercase letters to lowercase letters and vice versa.
|
<stdio.h> |
It is used to perform input and output operations using functions like scanf(), printf(), etc. |
<setjump.h> |
It contains standard utility functions like malloc(), realloc(), etc. It contains function prototypes for functions that allow bypassing
of the usual function call and return sequence.
|
<string.h> |
It is used to perform various functionalities related to string manipulation like strlen(), strcmp(), strcpy(), size(), etc. |
<limits.h> |
It determines the various properties of the various variable types. The macros defined in this header limits the values of
various variable types like char, int, and long. These limits specify that a variable cannot store any value
beyond these limits, for example, an unsigned character can store up to a maximum value of 255.
|
<time.h> |
It is used to perform functions related to date() and time() like setdate() and getdate(). It is also used to modify the system date
and get the CPU time respectively.
|
<stddef.h> |
It contains common type definitions used by C for performing calculations. |
<locale.h> |
It contains function prototypes and other information that enables a program to be modified for the current locale on which it’s running.
It enables the computer system to handle different conventions for expressing data such as times, dates, or large numbers throughout the world.
|
Example
The below example demonstrates the use of some commonly used header files in C.
C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s1[20] = "12345" ;
char s2[10] = "Geeks" ;
char s3[10] = "ForGeeks" ;
long int res;
res = pow (9, 3);
printf ( "Using math.h, "
"The value is: %ld\n" ,
res);
long int a = atol (s1);
printf ( "Using stdlib.h, the string" );
printf ( " to long int: %ld\n" , a);
strcpy (s2, s3);
printf ( "Using string.h, the strings"
" s2 and s3: %s %s\n" ,
s2, s3);
return 0;
}
|
Output
Using math.h, The value is: 729
Using stdlib.h, the string to long int: 12345
Using string.h, the strings s2 and s3: ForGeeks ForGeeks
2. Non-Standard Header Files in C and Their Uses
Non-standard header files are not part of the language’s ISO standard. They are generally all the header files defined by the programmers for purposes like containing custom library functions etc. They are manually installed by the user or maybe part of the compiler by some specific vendor.
There are lots of non-standard libraries for C language. Some commonly used non-standard/user-defined header files are listed below:
Header File |
Description |
<conio.h> |
It contains some useful console functions. |
<gtk/gtk.h> |
It contains GNU’s GUI library for C. |
Example
The below example demonstrates the use of conio.h non-standard header file.
C
#include <stdio.h>
#include <conio.h>
void displayMessage() {
printf ( "Hello! Geek\n" );
}
int main() {
printf ( "Press any key to print message \n" );
getch();
displayMessage();
return 0;
}
|
Output
Press any key to print message
Hello! Geek
Create your own Header File in C
Instead of writing a large and complex code, we can create our own header files and include them in our program to use whenever we want. It enhances code functionality and readability. Below are the steps to create our own header file:
Step 1: Write your own C code and save that file with the “.h” extension. Below is the illustration of the header file:
C
int sumOfTwoNumbers( int a, int b)
{
return (a + b);
}
|
Step 2: Include your header file with “#include” in your C program as shown below:
C
#include "iostream"
#include "sum.h"
using namespace std;
int main()
{
int a = 13, b = 22;
printf ( "Sum is: %d" , sumoftwonumbers(a, b));
}
|
Output
Sum is: 35
Including Multiple Header Files
You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.
Syntax
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
the entire header file
#endif
This construct is called wrapper “#ifndef”. When the header is included again, the conditional will become false, because HEADER_FILE_NAME is defined. The preprocessor will skip over the entire file contents, and the compiler will not see it twice.
Sometimes it’s essential to include several diverse header files based on the requirements of the program. For this, multiple conditionals are used.
Syntax
#if SYSTEM_ONE
#include "system1.h"
#elif SYSTEM_TWO
#include "system2.h"
#elif SYSTEM_THREE
....
#endif
Last Updated :
11 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...