#include vs #include “filename” in C++

Hello, Coders!! In this section, we will discuss the difference between #include <filename> and #include “filename” in a C++ program.

Let’s understand what does it mean #include <filename> or #include “filename” in a C++ program before discussing the differences between them.

#include directives in C++

We use #include to include a user-defined or standard header file to our program. It has mostly been written at the start of any C/C++ program.

The preprocessor read the directive, which attaches or inserts all the required modules from the mentioned user-defined or standard header files to the program. This process is called File Inclusion.

By using #include we can include the following two types of files:

1. Standard File or Standard File:


This type of file contains the various macro definitions and C/C++ function declaration. For example, cin, cout, scanf(), print(), etc, and various other standard functions are available within different header files.

 

2. User-defined Files:


These types of files are very similar to the Standard files, but the only difference is that the programmer writes and defines the files themselves.

It is very useful if we have a frequently used function in a program. We can simply define our functions in a user-defined file and import them anywhere in the program without defining them every time.

Difference in between #include<filename> and #include”filename”

In general, the common difference between these two methods is that the preprocessor searches in different locations for the included file.

In #include<filename> searching takes place in an implementation-dependent manner by the preprocessor. It searches the directories which are pre-designated by the IDE/Compiler. Different directories can be appended to this list by using the -I option.

This method imports the standard header files to the program.

Example:

#include<iostream> // Here we are including the standard header file.
using namespace std;

int main() {

   cout << "Hello World"; //"cout" imported from the "iostream" standard header file.
   return 0;
}

Output:

Hello World

 

In #include”filename” the preprocessor searches in an implementation-defined manner. In other words, the preprocessor search for the modules in the equivalent directory as the directive contained file. Different directories can be prepended to the list of quote directories by using the -iquote option.

This method imports the user-defined header files to the program.

Example:

#include<iostream> // Importing the System Header File

#include"myheader.h" //Here we are including the user-defined header file

int main() {
{
  int a = module(5,2); // "module" function is defined in the "myheader.h".
  
  cout << "The modulus is " << a; //"cout" is defined in "iostream" standard file.
  return 0;

}

Output:

The modulus is 1

Hope you have learned the basic difference between the #include<filename> and #include”filename” used in the C++ program.

Happy Coding!!

You can also read, How to create your own header file in C++

Leave a Reply

Your email address will not be published. Required fields are marked *