What is #include in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
How to prepare for Datadog senior software engineer interview?
Why should we hire you at Apple?What kind of interview questions does Apple ask?
How to handle stress in coding interviews?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.