Difference between #include and #include “filename” in C/C++

The #include directive in C and C++ is important for including the code from the other files into a source file. There are two main forms of this directive: #include <filename> for including standard library headers and #include “filename” for including user-defined headers, and understanding the differences between these two forms is important for C and C++ programming. So, in this article, we will discuss the  #include <filename> and  #include “filename” and their differences.

Table of Contents:

Understanding #include <filename> in C/C++

The #include directive in C and C++ is used to include the contents of a file in the source code before the compilation, and when the angle brackets are used with this #include directive (#include <filename>), then the compiler considers it a standard library header file.

Example in C:

C

Output:

Example in C Output

The code shows how the standard input/output library <stdio.h> is included in C to use the printf function.

Example in C++:

Cpp

Output:

Example in C++ Output

The code shows how the standard input/output library < iostream> is included in C++ to use the std::cout function.

Understanding #include “filename” in C/C++

The #include “filename” directive in C and C++ is used to include the user-defined header files. While the #include <filename> directive searches in system directories, this directive form searches in the working directory before looking anywhere else.

Note: When you are using the user-defined header files, please check that the compiler doesn’t look in additional included directories through the compiler flags.

Example in C:

File: mathutils.h

C

File: main.c

C

Output:

Understanding #include Output

The example shows how the mathutils.h header in C defines a function add(int a, int b) and main.c includes this header to implement the function add and prints the sum of 3 and 5.

Example in C++:

File: utilities.h

Cpp

File: utilities.cpp

Cpp

File: main.cpp

Cpp

Output:

Understanding #include Output

The example shows how the utilities.h is used in C++ to declare the greet function, utilities.cpp is used to define and print the greeting, and the main.cpp file implements the add function.

#include <filename> vs #include "filename"

Aspect #include <filename> (Angle Brackets)#include "filename" (Quotes)
UsageStandard library headersUser-defined/project headers
Search PathSystem directories firstCurrent directory first, then system directories
File LocationPredefined system pathsProject directory or specified paths
CustomizationCannot include custom filesCan include both standard and custom headers
Example#include <iostream>#include "myheader.h"
Risk of ConflictLow (system headers are unique)Higher (if a local file has the same name as a system header)
Compiler BehaviorSkips current directory, searches system paths directlyChecks current directory first, then system paths

Conclusion

For efficient C and C++ programming, it is important to understand the differences between the #include <filename> and  #include "filename". The #include <filename> is used for the standard library headers, which searches the system directories, while the  #include "filename" is used for the user-defined headers, which prioritizes the current directory. So, by understanding the differences and with the help of best practices, you can easily write a C or C++ program.

FAQs on Difference between #include <filename> and #include "filename" in C/C++

1. What does #include do?

The #include includes standard library headers and searches system directories.

2. What does #include "filename" do?

The #include “filename” includes user-defined headers and searches the current directory first.

3. Can I use #include "filename" for standard headers?

Yes, you can use #include “filename” for standard headers, but it may lead to naming conflicts with the local files.

4. What if a file isn't found with #include "filename"?

If a file isn’t found with #include “filename”, then the compiler will search system directories.

5. How can I avoid conflicts with standard headers?

You can avoid conflicts with the standard headers by using unique names for user-defined headers and organizing them into specific directories.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner