#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/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/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/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
There are two variations of how we can use #include in our C/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.
Examples of #include
Example 1
The below code shows the import of a system I/O header or standard file.
C
#include <stdio.h>
int main()
{
printf ( "hello world" );
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" ;
return 0;
}
|
Example 2
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
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);
}
|
Created the main file where the above “process.h” will be included.
C
#include <stdio.h>
#include "process.h"
int main()
{
add(10, 20);
multiply(10, 20);
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Jun, 2023
Like Article
Save Article