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.
#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
|
You are developing a program for selling in the open market. Some customers may insist on having certain additional features. However, you would like to have a single program that would satisfy both types of customers.
This situation is similar to the above situation and therefore the control directives take the following form:
1
2
3
4
5
|
#idef ABC
Group–A lines
#else
Group–B lines
#endif
|
Group-A lines are included if the customer ABC is defined. Otherwise, Group-B lines are included.
Situation 4
Suppose if you want to test a large program, you would like to include print calls in the program in certain places to display intermediate results and messages in order to trace the flow of execution and errors.
For this purpose we can use #if and #else directive as shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#if constant expression
{
Statement 1;
Statement 2;
——–
}
else
{
Statement 1;
Statement 2;
——–
}
#endif
|
Take your time to comment on this article.
Comments
Post a Comment