What are the Preprocessor Directives in C programming

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:

#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:
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:
When the #error directive is encountered by the compiler, it displays the error message and terminates the program.
Example:

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:
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:
and somewhere in the program the statement is written as: sum(a+b); then the preprocessor converts this line as shown below:

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.


EmoticonEmoticon