C/C++ #include directive with Examples

#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. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

  1. Header File or Standard files: This is a file which contains C/C++ 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 header files. So to utilise those functions, the users need to import a few header files which define the required functions.
  2. User-defined files: These files resembles 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. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor.

Syntax:

  1. #include "user-defined_file"

    Including using ” “: When using the double quotes(” “), the preprocessor access the current directory in which the source “header_file” is located. This type is mainly used to access any header files of the user’s program or user-defined files.

  2. #include <header_file>

    Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories.

Example 1: This shows the import of a system I/O header or standard file.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program showing the header file including
// standard input-output header file
  
#include <iostream>
using namespace std;
  
int main()
{
  
    // "cout" belongs to "iostream"
    cout << "hello world";
    return 0;
}

chevron_right


Output:

hello world

Example 2: This shows the creation and import of user-defined file.

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  2. Created the main file where the above “process.h” will be included.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
      
        // mult function defined in process.h
        mult(10, 20);
      
        // printf defined in stdio.h
        printf("Process completed");
        return 0;
    }

    chevron_right

    
    

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 ” ” one needs to can save it 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.