In C and C++ programming languages, the #include directive is used to include the contents of another file into your source code. There are two forms of including files: #include <filename> and #include "filename". The difference lies in how the preprocessor searches for the file to include.
#include <filename>: This form is used to include system header files. The preprocessor searches for the specified file in the standard system include paths. These paths are usually set by the compiler and contain directories where system header files are located.
#include "filename": This form is used to include user-defined header files or files that are located in the same directory as the source file. The preprocessor first searches for the specified file in the current directory and then, if not found, in the standard system include paths.
Here's an example to illustrate the difference:
Suppose you have a source file named main.c and two header files, sysheader.h and userheader.h, located in different directories:
lua
project_directory/
|-- main.c
|-- include/
| |-- sysheader.h
|-- src/
| |-- userheader.h
Contents of sysheader.h:
c
#ifndef SYSHEADER_H
#define SYSHEADER_H
void systemFunction();
#endif
Contents of userheader.h:
c
#ifndef USERHEADER_H
#define USERHEADER_H
void userFunction();
#endif
Contents of main.c:
c
#include <stdio.h> // Standard system header
#include "include/sysheader.h" // User-defined header located in the include directory
#include "src/userheader.h" // User-defined header located in the src directory
int main() {
printf("Main program\n");
systemFunction();
userFunction();
return 0;
}
In this example:
The #include <stdio.h> directive includes a standard system header.
The #include "include/sysheader.h" directive includes a user-defined header located in the include directory using the #include "filename" form.
The #include "src/userheader.h" directive includes a user-defined header located in the src directory using the #include "filename" form.
Both forms of #include serve different purposes and help you organize your code by separating declarations into different files. The choice of which form to use depends on whether you're including system headers or user-defined headers.
#include <filename>: This form is used to include system header files. The preprocessor searches for the specified file in the standard system include paths. These paths are usually set by the compiler and contain directories where system header files are located.
#include "filename": This form is used to include user-defined header files or files that are located in the same directory as the source file. The preprocessor first searches for the specified file in the current directory and then, if not found, in the standard system include paths.
Here's an example to illustrate the difference:
Suppose you have a source file named main.c and two header files, sysheader.h and userheader.h, located in different directories:
lua
project_directory/
|-- main.c
|-- include/
| |-- sysheader.h
|-- src/
| |-- userheader.h
Contents of sysheader.h:
c
#ifndef SYSHEADER_H
#define SYSHEADER_H
void systemFunction();
#endif
Contents of userheader.h:
c
#ifndef USERHEADER_H
#define USERHEADER_H
void userFunction();
#endif
Contents of main.c:
c
#include <stdio.h> // Standard system header
#include "include/sysheader.h" // User-defined header located in the include directory
#include "src/userheader.h" // User-defined header located in the src directory
int main() {
printf("Main program\n");
systemFunction();
userFunction();
return 0;
}
In this example:
The #include <stdio.h> directive includes a standard system header.
The #include "include/sysheader.h" directive includes a user-defined header located in the include directory using the #include "filename" form.
The #include "src/userheader.h" directive includes a user-defined header located in the src directory using the #include "filename" form.
Both forms of #include serve different purposes and help you organize your code by separating declarations into different files. The choice of which form to use depends on whether you're including system headers or user-defined headers.
Comments
Post a Comment