Preprocessor Directive: #include
The #include directive in C is used to include header files in a program. Header files contain declarations of functions and variables that are used in a program. The #include directive tells the compiler to insert the contents of the specified header file at the location of the directive.
all_matchThe preprocessor replaces the #include directive with the contents of the specified file. After including the file, the entire contents of the file can be used in the program.
all_match If the filename is in double quotes, first it is searched in the current directory (where the source file is present), if not found there then it is searched in the standard include directory.
all_match If the filename is within angle brackets, then the file is searched in the standard include directory only. The specification of the standard includes the directory implementation-defined.
#include directive - syntax
#include
Here is an example of using the #include directive to include the standard input/output library header file "stdio.h".
#include
int main() {
printf("Hello, world!");
return 0;
}
In the above code, the #include directive includes the "stdio.h"
header file, which contains the declaration of the printf() function
used in the program.
The #include directive can also be used to include user-defined header files.
For example, if we have a header file named "myheader.h"
that contains some function declarations, we can include it in our program like this -
#include "myheader.h"
int main() {
// call functions declared in myheader.h
return 0;
}
In this case, the header file "myheader.h"
is included using double quotes, since it is a user-defined header file.
The #include directive is a powerful feature in C that allows for modularity and code reuse. Header files are commonly used to define function prototypes and data structures that are used across multiple source files.
By including a header file, a programmer can use the declarations contained in it without having to redeclare them in each source file.