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
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

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 Meta so rich?
Will technology replace jobs in the future?
Which platform is good for coding?
How do I find and restore a deleted file in a Git repository?
What is portfolio and example?
What is the salary of analyst 1 in Coinbase?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

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.
4.1
Discounted price for Your Region

$72

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