|
C++ Programming Tutorials
Preprocessor
directives
Preprocessor directives are lines included in the code of our programs
that are not program statements but directives for the preprocessor.
These lines are always preceded by a pound sign (#). The preprocessor is
executed before the actual compilation of code begins, therefore the
preprocessor digests all these directives before any code is generated
by the statements.
These preprocessor directives extend only across a single line of code.
As soon as a newline character is found, the preprocessor directive is
considered to end. No semicolon (;) is expected at the end of a
preprocessor directive. The only way a preprocessor directive can extend
through more than one line is by preceding the newline character at the
end of the line by a backslash (\).
macro definitions (#define,
#undef)
To define preprocessor macros we can use #define. Its format is:
#define identifier replacement
When the preprocessor encounters this directive, it replaces any occurrence of
identifier in the rest of the code by replacement. This replacement can be an
expression, a statement, a block or simply anything. The preprocessor does not
understand C++, it simply replaces any occurrence of identifier by replacement.
#define TABLE_SIZE 100
int table1[TABLE_SIZE];
int table2[TABLE_SIZE]; |
After the preprocessor has replaced TABLE_SIZE, the code
becomes equivalent to:
int table1[100];
int table2[100]; |
This use of #define as constant definer is already known by
us from previuos tutorials, but #define can work also with parameters to define
function macros:
| #define getmax(a,b) a>b?a:b |
This would replace any occurrence of getmax followed by two
arguments by the replacement expression, but also replacing each argument by its
identifier, exactly as you would expect if it was a function:
// function macro
#include <iostream>
using namespace std;
#define getmax(a,b) ((a)>(b)?(a):(b))
int main()
{
int x=5, y;
y= getmax(x,2);
cout << y << endl;
cout << getmax(7,x) << endl;
return 0;
} |
5
7
|
Defined macros are not affected by block structure. A macro
lasts until it is undefined with the #undef preprocessor directive:
#define TABLE_SIZE 100
int table1[TABLE_SIZE];
#undef TABLE_SIZE
#define TABLE_SIZE 200
int table2[TABLE_SIZE]; |
This would generate the same code as:
int table1[100];
int table2[200]; |
Function macro definitions accept two special operators (#
and ##) in the replacement sequence:
If the operator # is used before a parameter is used in the replacement
sequence, that parameter is replaced by a string literal (as if it were enclosed
between double quotes)
#define str(x) #x
cout << str(test); |
This would be translated into:
The operator ## concatenates two arguments leaving no blank
spaces between them:
#define glue(a,b) a ## b
glue(c,out) << "test"; |
This would also be translated into:
Because preprocessor replacements happen before any C++
syntax check, macro definitions can be a tricky feature, but be careful: code
that relies heavily on complicated macros may result obscure to other
programmers, since the syntax they expect is on many occasions different from
the regular expressions programmers expect in C++.
NEXT >>
Conditional inclusions
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers.
|