Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Include Syntax
3.
How include works
4.
Once Only Headers
5.
Creating Our Own Header File
6.
Standard Header Files
7.
Frequently asked questions
8.
Key takeaways
Last Updated: Mar 27, 2024

Header Files

Author Shivam Verma
2 upvotes
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

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, C Static Function

Include Syntax

With the help of using preprocessing directives #include we can include both the user-defined and system-defined header files in a program.

There are two syntaxes of including a header file:

Syntax 

#include<file> 

The above syntax is used for the header file that is defined within the predefined source path. 

Syntax

#include “file” 

The above syntax is used for the header file that is not defined within the predefined source path. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

 

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;
}

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 read about C dynamic array, Tribonacci Series

Standard Header Files

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

  • #include<stdio.h>: This header file is used for performing input and output operations with the help of printf() and scanf() function.
  • #include<iostream>: This header file is used to perform input and output operations with the help of cin and cout objects.
  • #include<string.h>: This header file is used for performing various functionalities related to string manipulation like strcmp(), strlen(), strcpy() etc.
  • #include<math.h>: This header is used to perform mathematical operations such as sqrt(), pow(), log2(), etc.
  • #include<signal.h>: This header file is used for performing the signal handling functions like signal(), raise().
  • #include<errno.h>: This header file is used for performing the error handling related operations like errno(), perror(), strerror(), etc.
  • #include<iomanip.h>: This header file contains the definition of set() and setprecision() function to limit the decimal places in variables.
  • #include<stdarg.h>: This header file is used for performing standard argument functions like va_start() and va_arg().

Must Read, odd or even program in c

Frequently asked questions

  1. What is the header file in C Language?
    Ans: A file with extension .h is called a header file,  which contains C function declarations and macro definitions and is shared between several source files.
     
  2. What is the user-defined header file extension in C Language?
    Ans: .h extensions are used for user-defined header files.
     
  3. Which keyword is used to declare the header file?
    Ans: The include keyword is used to include the header file.
     
  4. Which header file is required for creating and reading data files?
    Ans: fstream.h header file is required for creating and reading data files.
     
  5. Which header file must be included to use stringstream?
    Ans: <string> header file must be included to use stringstream.

Key takeaways

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!

Related Articles: Bit stuffing program in c

 

Live masterclass