Preprocessor Directives
The C++ preprocessor is a program that is executed before the source code is compiled.
A preprocessor command is called directives. It begins with a hash symbol (#). No white space should appear before the # and a semi colon is not required at the end of the statement.
Things that can be done during the preprocessing stage are:
- Inclusion of the header file through #include directive
- Definition of a symbolic constant and macros through #define directive
The #define preprocessor allows us to define symbolic names and constants.
For example: #define PI 3.14159
This statement will translate every occurrence of PI in the program to 3.14159. Now consider the complete example where in every occurrence of PI will be replaced with the value 3.14159, the value defined with #define directive.
The #define preprocessor allows you to make text substitution before compiling the program.
An example:
Before compilation, if the C++ preprocessor finds MAX as single token, in the source code, it replaces it with the number 70. If MAX was part of a string e.g. MAXIMUM, the preprocessor will not process on it.
Every time the preprocessor sees NAME, in the source code, it will replace it with "computer science C++".
Macros are built on the #define preprocessor directive.
The main difference between the two is that one is a symbolic constant while the other is an expression.
A few things that should be known about macros,
- Macro without any arguments is treated like a symbolic constant.
- Macro substitutes text only, it does not check for data type.
Comments - No Responses to “Preprocessor Directives”
Sorry but comments are closed at this time.