Table of contents
1.
Introduction 
2.
What is a header file?
3.
Syntax of Header Files in C
4.
Standard Header Files
5.
Types of C Header Files
6.
How include works
7.
Once Only Headers
8.
Creating Our Own Header File
9.
Including Multiple Header Files
10.
Standard Header Files
10.1.
Example of Header File in C
10.2.
C
11.
Frequently asked questions
11.1.
What is the user-defined header file extension in C Language?
11.2.
Which keyword is used to declare the header file?
11.3.
Which header file is required for creating and reading data files?
11.4.
Which header file must be included to use stringstream?
12.
Conclusion
Last Updated: Sep 6, 2024
Easy

Header Files in C

Author Shivam Verma
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

A file with extension “.h” is called a header file, which holds the definitions of various functions and their associated variables that need to be imported into your C program with the help of the preprocessor #include statement. Header files are included in a source program during compilation. Including a header file in your program means using the header file's content in your source program.

Header Files in C

There are two types of header files:

  1. User-defined header file: User-defined header files are created by the user and can be imported using "#include".
  2. System-defined header file: The header file the user does not define is known as a system-defined header file. These header files are already available in the C/C++ compiler, and we just need to import them. 

Also See, Sum of Digits in C

What is a header file?

A header file is a file in programming that contains declarations for functions, variables, and other identifiers. It is commonly used to share these declarations between multiple source files in a program. Header files help organize code by separating the implementation (in source files) from the declarations (in header files). In languages like C and C++, header files typically have the extension .h and are included in source files using the #include directive.

Syntax of Header Files in C

#include <header_file_name.h>


For example, to include the standard input-output header file in C:

#include <stdio.h>

Standard Header Files

There are some header files, and their uses are given below:

Header FileDescription
#include<stdio.h>Used for performing input and output operations with functions like printf() and scanf().
#include<iostream>Used to perform input and output operations with objects like cin and cout.
#include<string.h>Used for string manipulation functions like strcmp(), strlen(), strcpy(), etc.
#include<math.h>Used for mathematical operations like sqrt(), pow(), log2(), etc.
#include<signal.h>Used for signal handling functions like signal(), raise().
#include<errno.h>Used for error handling operations like errno(), perror(), strerror(), etc.
#include<iomanip.h>Contains the definition of set() and setprecision() to limit decimal places in variables.
#include<stdarg.h>Used for standard argument functions like va_start() and va_arg().

Types of C Header Files

In C programming, there are two types of header files:

Standard Library Header Files
These header files come with the C compiler and are predefined. They provide common functionalities like input/output operations, string manipulation, and mathematical operations. Examples include stdio.h, string.h, and math.h.

User-Defined Header Files
These header files are created by the programmer to store custom functions and macros that can be reused in multiple programs. They are included in a program using the #include "filename.h" directive.

How include works

To understand how include works let us suppose that we have a header file “ninjas.h” that contains the following statement:

char *Example(void);

 

then, we have a main C program prog.c, which uses the header file as shown below:

#include<stdio.h>
#include "ninjas.h"
int main() 
{
    printf("Coding Nijas");
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

The compiler would replace the definition of ninjas.h header file as shown below:

#include<stdio.h>
char *Example(void);
int main() 
{
    printf("Coding Nijas");
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Run the code with the help of online C compiler.

Once Only Headers

If the header file in a program is included twice, then the compiler will process the contents of that header file twice, eventually leading to an error in your program. To get rid of this problem, you have to use conditional preprocessor directives. Here is the syntax:

Syntax

#ifndef My_Header_File  
#define My_Header_File 

   the entire header file

#endif

This construct is called wrapper "#ifndef". The conditional will become false when the header is included again because My_Header_File is defined. The preprocessor will skip over the entire file contents, and the compiler will ignore the second declaration of the header file. 

Sometimes it is essential to include several diverse header files based on the requirements of the program. For this, multiple conditional preprocessors are used like this:

#if First_System
   #include "system1.h"
#elif Second_System
   #include "system2.h"
#elif Third_System
   ....
#endif

Also read, Short int in C Programming

Creating Our Own Header File

A header file is used to avoid writing large and complex code. When you create your own header file, you can simply use it wherever you want. Header files enhance code readability and functionality.

The following steps to create our own header file are given below:

1. First, you have to write your own C code and save it with the .h extension. The example of creating your own header file is given below.

// function to add two numbers passed  
int Addition_of_numbers(int x, int y)  
{
    int sum=x+y;
    return (sum);  
}

 

Suppose the name of the above header file is addition.h.

2. Second step is to include your header file with “#include” in your C program as shown below.

// C program to calculate the addition of two numbers using function declared in header file  
#include<stdio.h>  
// including header file   
#include "addition.h"
int main()  
{  
    int x=15,y=10;
    // function declared in addition.h header file to calculate the addition.
    printf("Result of addition is : %d", Addition_of_numbers(x, y));  
}
You can also try this code with Online C Compiler
Run Code


You can also read about C dynamic array

Including Multiple Header Files

In C programming, you can include multiple header files in a single program by using multiple #include directives. This allows you to use various functions and features from different libraries. For instance, if you need both input/output functions and string manipulation, you can include both stdio.h and string.h. The syntax would look like this:

#include<stdio.h>

#include<string.h>


Make sure to avoid redundant inclusion by using include guards or #pragma once.

Standard Header Files

There are some header files, and their uses are given below:

Header FileDescription
#include<stdio.h>Used for performing input and output operations with functions like printf() and scanf().
#include<iostream>Used for input and output operations in C++ with objects like cin and cout.
#include<string.h>Provides string manipulation functions like strcmp(), strlen(), and strcpy().
#include<math.h>Contains functions for mathematical operations such as sqrt(), pow(), log2(), etc.
#include<signal.h>Used for signal handling functions like signal() and raise().
#include<errno.h>Provides error handling functionalities such as errno(), perror(), and strerror().
#include<iomanip.h>Contains functions like set() and setprecision() to control decimal precision in variables.
#include<stdarg.h>Used for handling variable arguments with functions like va_start() and va_arg().


Must Read, odd or even program in c

Example of Header File in C

  • C

C

#include<stdio.h>
#include<string.h>

int main() {
char str1[20] = "Hello";
char str2[20];

// Using strcpy() from <string.h>
strcpy(str2, str1);

// Using printf() from <stdio.h>
printf("Copied String: %s\n", str2);

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

In this example, the program uses the stdio.h header for input/output operations and the string.h header for string manipulation.

Output

Copied String: Hello


Explanation:

  • The program copies the string "Hello" from str1 to str2 using the strcpy() function from the <string.h> header.
     
  • Then, it prints the copied string using the printf() function from the <stdio.h> header.

Frequently asked questions

What is the user-defined header file extension in C Language?

Ans: .h extensions are used for user-defined header files.

Which keyword is used to declare the header file?

Ans: The include keyword is used to include the header file.

Which header file is required for creating and reading data files?

Ans: fstream.h header file is required for creating and reading data files.

Which header file must be included to use stringstream?

Ans: <string> header file must be included to use stringstream.

Conclusion

In this blog, We learned about C header files and how these header files can be included in our C program, and how it works within our C program. This blog is over, but the thirst for learning is not over yet. Use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews. You can also visit and read our DSA blogs by clicking here. Till then, have a nice day and Happy Coding!

 

Live masterclass