Understanding the difference between #include <filename> and #include "filename" in C
Difference Between #include <filename> and #include "filename" in C
In C language, the #include directive is used to include the contents of a file into a program, typically for accessing standard libraries or custom headers. However, there is a difference between using angle brackets (<>) and double quotes ("") when specifying the file name in the #include directive.
#include <filename>
- Purpose: Primarily used for including standard library headers (e.g., <stdio.h>, <stdlib.h>).
- Behavior: Instructs the compiler to look for the header file in system directories only. These are directories specified in the compiler's predefined include paths.
#include "filename"
- Purpose: Primarily used for including user-defined or local headers (e.g., "testheader.h").
- Behavior: Instructs the compiler to first look for the file in the current directory (the directory of the source file). If the file is not found there, it then searches in the system directories.
Here is an example that includes both a standard library header and a user-defined header.
Code:
#include <stdio.h> // Standard library header
#include "testheader.h" // Local or user-defined header
int main() {
printf("Using standard library with #include <stdio.h>\n");
myFunction(); // Function defined in myheader.h
return 0;
}
Suppose myheader.h is defined as follows:
Code:
// testheader.h
void myFunction() {
printf("Using a user-defined header with #include \"testheader.h\"\n");
}
Output:
Using standard library with #includeUsing a user-defined header with #include "testheader.h"
Explanation:
- #include <stdio.h> includes the standard input-output library, found in the system directories.
- #include "myheader.h" includes the custom header file from the current directory. If not found, it would then check the system directories.
Summary
- #include <filename> is used for standard libraries, searching only in system directories.
- #include "filename" is used for user-defined headers, searching the current directory first.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics