What Is the Difference Between #include and #include “filename”?

1. Overview

In C programming, the #include directive is used to insert the contents of a specified file into the source code during compilation. This is essential for reusing code, such as standard library functions or custom header files. The choice between #include <filename> and #include “filename” determines where the compiler looks for the file and how it handles the inclusion.

In this tutorial, we’ll explore how to use the two directives and then see what they differ from.

2. #include <filename>

To begin,  #include <filename> syntax is primarily used for including standard library headers or system headers. When using this syntax, the compiler searches for the file in a predefined list of directories.

It’s ideal for adding a standard library such as <stdio.h>, <stdlib.h>, etc.

Here’s an example:

#include <stdio.h>

In the code above, we added a library that provides functions for file handling, input, and output

3. #include “filename”

Unlike #include <filename>, the #include “filename” syntax helps to include a custom header we define ourselves. When using this syntax, the compiler first searches for the file in the current directory (the directory where the source file is located). If the file is not found, it falls back to searching in the standard include directories.

For example, let’s define a header file named constants.h:

#ifndef CONSTANTS_H 
#define CONSTANTS_H

const int MAX_VALUE = 100;

#endif

Next, let’s create the main.c file and reference our custom header:

/#include 
#include "constants.h"

int main() {
    printf("The maximum value is: %d\n", MAX_VALUE);
    return 0;
}

In the code above we use the #include direct to import our custom header into the current code. Notably, the custom header is in the same directory as the main.c file. In a case, where they are not in the same directory, we need to specify the path of the header file.

4. Conclusion

In this tutorial, we learned the basics of #include <filename> and #include “filename” directives. Understanding their difference is crucial for writing organized and efficient C/C++ code. We use #include <filename> for standard or system headers and #include “filename” for our own header files.

Leave a Reply

Your email address will not be published. Required fields are marked *