What is #include in C++?

In C++, #include is a preprocessor directive used to include the contents of a file (usually a header file) in the program before compilation. This allows you to access functions, classes, and other definitions from libraries and modules without having to redefine them.

Key Points about #include:

  1. Header Files: The most common use of #include is to include standard library header files (e.g., <iostream>, <vector>, <string>) or user-defined header files. For example:

    #include <iostream> // Standard library for input-output operations #include "myHeader.h" // User-defined header file
  2. Angle Brackets vs. Quotes:

    • Angle Brackets (<>): Used for including standard library headers or system files. The compiler searches for these files in standard library directories.
    • Quotes (""): Used for including user-defined header files. The compiler first searches in the current directory and then in standard directories.
  3. Compile-Time Inclusion: The contents of the included file are copied into the source file at the point where #include is used, allowing the program to utilize the definitions from the included file.

  4. Avoiding Multiple Inclusions: To prevent multiple inclusions of the same header file, which can lead to errors, it's common to use include guards or #pragma once:

    // Using include guards #ifndef MYHEADER_H #define MYHEADER_H // Declarations #endif
  5. Library Functions: Including libraries using #include gives you access to pre-written functions and classes, facilitating code reuse and improving development efficiency.

Example Usage:

Here’s a simple example that demonstrates the use of #include:

#include <iostream> // Include the iostream library using namespace std; int main() { cout << "Hello, World!" << endl; // Outputs: Hello, World! return 0; }

Conclusion:

The #include directive is fundamental in C++ programming, allowing developers to include and utilize code from external libraries and their own header files, thus promoting modular programming and code reuse.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Why is HackerRank free?
Is it OK to Google during coding interview?
What is Meta best known for?
What qualifications do you need to work at Netflix?
Which software tool is trending now?
What is the rule order in Zscaler?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.