Preprocessor Directive: "#include"
Preprocessor Directives
- Commands that are given to the preprocessor are called Preprocessor Directives.
- All preprocessor commands / directives begin with pound sign (also known as hash symbol - #). Since they are not C/C++ commands, therefore, never put semicolon at the end of a preprocessor directive.
- These commands are given to the C/C++ preprocessor to change the source code. But the changes last only as long as the compile takes place. Once the code is complied and converted into program, the original source code is restored.
- Its important to remember that the preprocessor does not in any way compile your program or look at the actual C/C++ statements.
- Preprocessor directives are the special non-C/C++ commands, that make changes to your source code before the compiler start its work.
- First the preprocessor is activated to execute the preprocessor directives/commands, then the compilation process takes.
- So by Preprocessor we mean, the changes are made to our source code before it is compiled. The original source code is restored as soon as the compile is finished. When we look at our program again, it is back in its original form.
File Inclusion Directive
- The #include preprocessor directive merges a disk file into your source program.
- When preprocessor finds this directive, it replace the directive with the entire contents of the specified file.
- The file might be C/C++ header file like stdio.h, conio.h, iostream etc; or
- It might be any other outside file containing boilerplate text stored on the disk.
- There are two ways to use #include directive.
- Using angle brackets (< >): The angle brackets tell the preprocessor to look for the include in a default include director, which is set by the compiler. Written as #include <filename>
- Using double quotes (" "): The double quotes notify the preprocessor to look for the include file in the same directory where the source code file is stored; and if not found there, then look in the system's include directory. Written as #include "filename"
Example:
#include <iostream>
void main( ) {
#include "myfile.cpp"
// where myfile.cpp can be any of your file,
/*remember, inside function, not to put semicolon (;) at the end */
}
References:
- Turbo C++ By Example. by Greg Perry and Marcus Johnson (1992)
- C - Preprocessors
- Preprocessor directives
Comments
Post a Comment