The C Preprocessor is a tool that is used to perform the pre-processing part before the actual compilation begins. Since the C compiler itself uses the C preprocessor to transform the source code before compiling, we do not tend to distinguish the pre-processing step.
Having noticed that people confuse preprocessor in C and C++, I would like to clear that the C preprocessor is a tool which is used both by C and C++ compiler.
The C preprocessor is intended to perform certain tasks which form the pre-processing step. The C preprocessor can be invoked by using preprocessor directives. Processor directives usually start with # symbol. Processor directive is in a way, a preprocessor command that instructs it to perform some action. The two most commonly used preprocessor directives are:
- #include - Include the code/text from the files specified with
- #define - Macro expansion or substitution.
The #include directive
In this post, I'll mostly be talking about the specifics and semantics of the #include preprocessor directive.
#include is used in C++ and C as a preprocessor directive. #include instructs the preprocessor to perform the task of file inclusion.
For example, if you specify #include<file> in the start of your C/C++ file called 'myfile.cpp', it will instruct the preprocessor to substitute the contents of 'file' in 'myfile.cpp' before continuing to process the next statement in 'myfile.cpp'.
The variants of #include:
-
#include<systemhfile>
'the include with brackets' lets you include the system header files into your program.
The include search path i.e. the path where the systemhfile will be searched is:
First, the directories specfied by the user are searched. The user can specify the directories using -I option of gcc or g++.Second, the systemhfile is searched in a standard list of system directories. These are usually the /include directories such as /usr/local/include/, /usr/include/ etc. The directories in C or C++ include search path can be found by the following commands:
C++: gcc -xc++ -E -v -
C: gcc -xc -E -v -
-x option specifies the language.
-E option tells gcc to stop after the preprocessing stage.
-v tells to print on std output.
Also, note that the order of the directories while searching for #include header files matter. For example, consider that a file named 'systemhfile'is present in both /usr/local/include/ and /usr/include/ and if in the output of the above command, if /usr/local/include/ comes before /usr/include/,
it means that the header file in /usr/local/include/ will be used in your program. -
#include "headerfile"
'the include with quotes' lets you include your own or custom header file into the program.
The #include path i.e. the path where the headerfile will be searched for:
First, in the current directory of your source file.
Second, in the same set of directories used for searching the system header file in point 1. -
#include anything_Else
'the include without double quotes or angle brackets' treats the argument anything_Else as a macro. This macro needs to be defined later in your source file. This macro itself should be of the form "file" or <file>. This is called computed include.
I have tried my best to include most useful details about the #include directive in the article.
The other directives will be covered in the coming posts. Do message for any suggestions or ideas about the posts.
Cheers !