Preprocessing Directives


C’s preprocessor provides a facility for defining constant and substitution, which are commonly called macros. It is used when you either want the make to program more readable or when you don’t have enough information about certain values. For example, if your input is in U.S. dollars, and your processing is done in terms of rupees, then your program may have the expression

Rs = usd * 46;

where 46 is the currency rate. You can write the expression as:

# define currency_rate 46
rs = usd * currency_rate;

So, if the currency rate is changed, you can make the necessary change only in one place. The preprocessor directive is defined here.

#include <stdio.h>

#define VAL 35        // A
#define HELLO "HELLO"      // B

int main ()
    int res;
    res = VAL-5;            // C
    printf("res = VAL-5: res == %d\n", res);
    printf(HELLO);        //D
    return 0;

Statements A and B indicate preprocessor directives. VAL is defined as integer 35 and HELLO is a string as “HELLO”. Whenever VAL and HELLO appear, they are replaced by the specified values.

In statement C, VAL 35 is replaced by VAL −5. So the statement becomes

res = 35-5;

Statement D, after replacement, becomes

printf ("HELLO")

The preprocessor directives are not C statements, so they do not end with semicolons.

The include directive tells the compiler to include all the contents of a specified file in the source file before giving the source file for compiling.

The preprocessor substitutes strings that are specified by using define directive

#define constant identifer "value"

Following are valid define expressions:

#define TRUE 1
#define FALSE 0
#define BS '\b'
#define TAB '\011'