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 <header_file_name.h>

Here is an example of using the #include directive to include the standard input/output library header file "stdio.h".

#include <stdio.h>
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.

Preprocessor Directive: #include_next

The #include_next preprocessor directive is a feature that is not commonly used in C programming, but it can be useful in certain situations.

This directive is used to include the next available header file with the same name as the currently included header file, starting from the directory following the current directory in the include path.

Suppose we have two header files, header1.h, and header2.h, and we want to include header2.h after header1.h in our program.

However, both files have the same name and are located in different directories. We can use the #include_next directive to ensure that the correct header file is included.


// main.c
#include "header1.h"
#include_next "header1.h"

Assuming that header1.h is located in the directory /dir1 and header2.h is located in the directory /dir2, we can set the include path to include /dir1 and /dir2 in that order.

When header1.h is included in main.c, the preprocessor will search for header2.h in /dir1, but it won't find it.

When #include_next is encountered, it will search for the next available header file with the same name, starting from /dir2.

Since header2.h is located in /dir2, it will be included.

One important thing to note is that the #include_next directive may not be supported by all compilers. It is a non-standard extension to the C language, and some compilers may not recognize it.

Another potential issue with using #include_next is that it can be difficult to predict which header file will be included, especially if the include path is not well-defined or if there are multiple header files with the same name in different directories.

Loading...

Search