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
// C program to demonstrate the use of header files
// standard input and output stdio.h header file
#include <stdio.h>
int main()
{
printf(
"Printf() is the function in stdio.h header file");
return 0;
}
OutputPrintf() 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. |
| <errno.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
// C program to illustrate
// the use of header file
// in C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver Code
int main()
{
char s1[20] = "12345";
char s2[10] = "Geeks";
char s3[10] = "ForGeeks";
long int res;
// Find the value of 9^3 using a
// function in math.h library
res = pow(9, 3);
printf("Using math.h, "
"The value is: %ld\n",
res);
// Convert a string to long long int
// using a function in stdlib.h library
long int a = atol(s1);
printf("Using stdlib.h, the string");
printf(" to long int: %ld\n", a);
// Copy the string s3 into s2 using
// using a function in string.h library
strcpy(s2, s3);
printf("Using string.h, the strings"
" s2 and s3: %s %s\n",
s2, s3);
return 0;
}
OutputUsing 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>
// Function to display a welcome message
void displayMessage() {
printf("Hello! Geek\n");
}
int main() {
// Using conio.h functions
printf("Press any key to print message \n");
getch(); // Wait for a key press
// Call the additional function after a key press
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
// Function to find the sum of two
// numbers passed
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
// C++ program to find the sum of two
// numbers using function declared in
// header file
#include "iostream"
// Including header file
#include "sum.h"
using namespace std;
// Driver Code
int main()
{
// Given two numbers
int a = 13, b = 22;
// Function declared in header
// file to find the sum
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
Similar Reads
C Preprocessors
Preprocessors are programs that process the source code before compilation. Several steps are involved between writing a program and executing a program in C. Let us have a look at these steps before we actually start learning about Preprocessors. You can see the intermediate steps in the above diag
10 min read
C Preprocessor Directives
In almost every C program we come across, we see a few lines at the top of the program preceded by a hash (#) sign. They are called preprocessor directives and are preprocessed by the preprocessor before actual compilation begins. The end of these lines is identified by the newline character '\n', n
8 min read
How a Preprocessor works in C?
Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan
3 min read
Header Files in C
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 functi
7 min read
What’s difference between header files "stdio.h" and "stdlib.h" ?
In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used
4 min read
How to write your own header file in C?
As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar
4 min read
Macros and its types in C
In C, a macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;). E
6 min read
Interesting Facts about Macros and Preprocessors in C
In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the ‘#’ symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before
6 min read
# and ## Operators in C
In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs. St
3 min read
How to print a variable name in C?
Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification. Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with
1 min read
Multiline macros in C
In this article, we will discuss how to write a multi-line macro. We can write multi-line macro same like function, but each statement ends with "\". Let us see with example. Below is simple macro, which accepts input number from user, and prints whether entered number is even or odd. #include <s
3 min read
Variable length arguments for Macros
Like functions, we can also pass variable length arguments to macros. For this we will use the following preprocessor identifiers.To support variable length arguments in macro, we must include ellipses (...) in macro definition. There is also "__VA_ARGS__" preprocessing identifier which takes care o
2 min read
Branch prediction macros in GCC
One of the most used optimization techniques in the Linux kernel is " __builtin_expect". When working with conditional code (if-else statements), we often know which branch is true and which is not. If compiler knows this information in advance, it can generate most optimized code. Let us see macro
2 min read
Difference between typedef and #define in C
In C, both typedef and #define can be used to create aliases for existing types or values, but their behaviors and implementation differ significantly. typedef is a keyword used to create type aliases(nicknames) that are checked by the compiler, while #define is a preprocessor directive that perform
3 min read
Difference between #define and const in C
In C, both #define and const define constant values, but these constants differ greatly in their behaviors and implementation. #define is a preprocessor directive used to define constants that are replaced by their value during preprocessing, before actual compilation begins. while const defines a t
3 min read