The ANSI committee has added some more preprocessor directives to the existing list. They are:
#elif Directive
The #elif directive enables us to establish an “if…else…if” sequence for testing multiple conditions. The syntax is as shown below:
1
2
3
4
5
6
7
|
#if expr1
Stmts;
#elif expr2
Stmts;
#elif expr3
Stmts;
#endif
|
#pragma Directive
The #pragma directive is an implementation oriented directive that allows the user to specify various instructions to be given to the compiler. Syntax is as follows:
1
|
#pragma name
|
Where name is the name of the pragma we want. For example, under Microsoft C, #pragma loop_opt(on) causes loop optimization to be performed.
#error Directive
The #error directive is used to produce diagnostic messages during debugging. The general format is:
1
|
#error error-message
|
When the #error directive is encountered by the compiler, it displays the error message and terminates the program.
Example:
1
2
3
|
#if !defined(FILE_G)
#error NO GRAPHICS FILE
#endif
|
Suppose a customer has two different types of computers and you are required to write a program that will run on both the systems. You want to use the same program, although a certain lines of code must be different for each system.
The main concern here is to make the program portable. This can be achieved as shown below:
1
2
3
4
5
6
7
8
9
10
11
|
#ifdef IBM_PC
{
——
——
}
#else
{
——
——
}
#endif
|
If we want to run the program on a IBM PC, we include the directive #define IBM_PC, otherwise we won’t.
Preprocessor Operations
Stringizing Operator #
ANSI C provides an operator # called stringizing operator to be used in the definition of macro functions. This operator converts a formal argument into a string. For example, if the macro is defined as follows:
1
|
#define sum(xy) printf(#xy “ = %f \n”, xy)
|
and somewhere in the program the statement is written as: sum(a+b); then the preprocessor converts this line as shown below:
1
|
printf(“a+b” “ = %f \n”, a+b);
|
Token Pasting Operator ##
The token pasting operator ## defined by ANSI enables us to combine two tokens within a macro definition to form a single token.
Take your time to comment on this article.
Comments
Post a Comment