Difference Between #include ‹filename› and #include 'filename'

In C++, the #include directive is used to include header files in a program. There are two common ways to include header files: using angle brackets (< >) and using double quotes (" "). The difference between #include <filename> and #include "filename" lies in the way the compiler searches for the header file.


#include <filename>

When you use angle brackets, the compiler searches for the header file in the standard system directories. These directories contain the standard library headers and other system-specific headers. Typically, these headers are not part of your program's source code but are provided by the compiler or the system.

Example Code:

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

In this example, #include <iostream> is used to include the standard input/output stream header file. The compiler knows where to find this file in the standard system directories.

#include "filename"

When you use double quotes, the compiler searches for the header file in the current directory or the directory specified by the -I flag (include directory) during compilation. This method is typically used for including user-defined header files or headers located within the project directory.

Example:
Suppose you are working on a C++ project that involves creating a game. You have a file called "game.h" that contains the declarations of classes, functions, and variables specific to your game. In order to use these declarations in your main program file, you would include the "game.h" header file using double quotes.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS