Open In App

#include in C

Improve
Improve
Like Article
Like
Save
Share
Report

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C program. The #include preprocessor directive is read by the preprocessor and instructs it to insert the contents of a user-defined or system header file in our C program. These files are mainly imported from outside header files.

The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This preprocessor directive tells the compiler to include a file in the source code program.

Types of Header Files

There are two types of files that can be included using #include:

1. Pre-Existing Header Files: The pre-existing header files come bundled with the compiler and reside in the standard system file directory. This file contains C standard library function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin, and various other input-output or other standard functions are contained within different Pre-Existing header files.

2. User-Defined Header Files: These files resemble the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times.

Syntax of #include in C

There are two variations of how we can use #include in our C program.

1. Including using <>

It is mainly used to access pre-existing system header files located in the standard system directories.

#include <header_file>

While importing a file using angular brackets(<>), the preprocessor uses a predetermined directory path to access the file.

2. Including using ” “

This type is mainly used to access any header files of the user’s program or user-defined files.

#include "user-defined_file"

When using the double quotes(” “), the preprocessor accesses the current directory in which the source “header_file” is located or the standard system directories.

To import the user-defined header file using #include, the file should be in a directory path relative to your C source file otherwise, the preprocessor will begin search for it in the standard system directory.

To know more refer to Difference between #include <> and #include “” 

Examples of #include in C

Example 1

The below code shows the import of a system I/O header or standard file.

C




// C program showing the header file including
// standard input-output header file
 
#include <stdio.h>
 
int main()
{
 
    // "printf()" belongs to stdio.h
    printf("hello world");
    return 0;
}


Output

hello world

Example 2

In the below example, the #include <math.h> directive allows us to use mathematical functions like sqrt for calculating the square root.

C




// C program to calculate square root of a number using the
// math library functions
 
#include <math.h> // Including math header for mathematical operations
#include <stdio.h>
 
int main()
{
    double num = 14.0;
    double Res = sqrt(num);
 
    printf("Square root of %.2f is %.2f\n", num, Res);
    return 0;
}


Output

Square root of 14.00 is 3.74

Example 3

The below code shows the creation and import of a user-defined file.

Creating a user-defined header by the name of “process.h”.

C




// It is not recommended to put function definitions
// in a header file. Ideally there should be only
// function declarations. Purpose of this code is
// to only demonstrate working of header files.
 
void add(int a, int b)
{
    printf("Added value=%d\n", a + b);
}
 
void multiply(int a, int b)
{
    printf("Multiplied value=%d\n", a * b);
}


Creating the main file where the above “process.h” will be included. 

C




// C program to illustrate file inclusion
// <> used to import system header file
#include <stdio.h>
 
// " " used to import user-defined file
#include "process.h"
 
// main function
int main()
{
    // add function defined in process.h
    add(10, 20);
 
    // multiply function defined in process.h
    multiply(10, 20);
 
    // printf defined in stdio.h
    printf("Process completed");
    return 0;
}


Explanation

Including the “process.h” file into another program. Now as we need to include stdio.h as #include in order to use printf() function similarly, we also need to include the header file process.h as #include “process.h”. The ” ” instructs the preprocessor to look into the present folder or the standard folder of all header files, if not found in the present folder.

If angular brackets are used instead of ” ” the compiler will search for the header file in the standard folder of header files. If you are using ” ” you need to ensure that the created header file is saved in the same folder in which the current C file using this header file is saved.

Conclusion

In C programming, the #include directive is very important to integrate any external files (header files) into a program. This directive is always placed at the beginning of a C program and it is a preprocessor command that tell the compiler to include the contents of the file specified after #include directive. There are two types of files that can be included: pre-existing system header files and user-defined header files. Overall this directive allows us to write a modular code, organize the code, and easy to reuse functionalities.



Last Updated : 08 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads