The C Preprocessor

C - Preprocessor

The C Preprocessor : The preprocessor is exactly what its name implies. It is a program that processes our source program before it is passed to the compiler. Preprocessor commands from what can almost be considered a language within C language. We can certainly write C programs without knowing anything about the preprocessor or its facilities. But preprocessor is such a great convenience that virtually all C programmers rely on it.

Features of C Preprocessor

There are several steps involved from the stage of writing a C program to the stage of getting it executed. The combination of these steps is known as the ‘Bulid Process’. The C program is often known as ‘Source Code’. The preprocessor works on source code and creates ‘Expanded Source Code’. If the source code is stored in a file PR1.C, then the expanded source code gets stored in a file PR1.I. It is expanded source code that is sent to the compiler for compilation.

The preprocessor offers several features called preprocessor directives. Each preprocessor directives begins with a # symbol. The directives can be placed anywhere in the program but are most often placed at the beginning of a program, before the first function definition. We would learn the following preprocessor directives here:

a) Macro expansion.
b) File  inclusion
c) Conditional compilation
d) Miscellaneous Directives

Macro Expansion

Have a look at the following program:

# include<stdio.h>
# define UPPER 25
int main()
int i;
return 0;

# define UPPER 25 this statement is called ‘macro expansion’ or more commonly, just a ‘macro’. What purpose does it serve ? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25.

NOTE: That the macro template and its macro expansion are separated by blanks or tabs. A space between # and define is optional. Remember that a macro definition is never to be terminated by a semicolon.

Macros with Arguments

The macros that we have used so far are called simple macros.Macros can have arguments, just as functions can. Here is an example that illustrates this fact.

# include <stdio.h>
# define AREA(x)(3.14*x*x)
int main()
float r1=6.25,r2=2.5,a;

printf(“Area of circle=%f\n”,a);
printf(“Area of circle=%f\n”,a);
return 0;


Area of circle=122.656250
Area of circle=19.625000

In this program, whenever the preprocessor finds the phrase AREA(x) it expands it into the statement (3.14*x*x).

File Inclusion:

The second preprocessor directives we’ll explore in this tutorial. This directives causes one file to be included in another. The preprocessor command for file inclusion looks like this:

# include “filename”

And it simply causes the entire contents of filename to be inserted into the source code at that point in the program. Of course, this presumes that the file begin included exists. It can be used in two cases:

i. If we have a very large program, the code is the best divided into several different files, each containing a set of related functions.

ii. There are some functions and some macro definitions that we need almost in all program that we write. These commonly needed functions and macro definitions can be stored in a file, and that file can be included in every program  we write, which would add all the statements in this file to our program as if we have typed them in.

Actually there exist two ways to write #include statement. These are :

# include “filename”
# include <file name>

Conditional Compilation

We can, if we want, have the compiler skip over part of source code by inserting the preprocessing commands #ifdef and #endif, which have the general form:

#ifdef macroname
Statement 1;
Statement 2;
Statement 3;

If  macroname has been #defined, the block of code will be processed as usual; otherwise not.

#if and #elif Directives

The  #if  directives can be used to test whether an expression evaluates to a nonzero value or not. If the result of the expression is nonzero, then subsequent lines upto a #else, #elif  or #endif are compiled, otherwise they are skipped.


int main()

# if TEST<=5
statement 1;
Statement 2;
Statement 3;

Statement 1;
Statement 2;
Statement 3;


Miscellaneous Directives

There are two more preprocessor directives available, there are two more preprocessor directives available, though they are not very commonly used. They are:

ii. #pragma

 #undef directive: On some occasions, it may be desirable to cause a defined name to become ‘undefined’. This can be accomplish by means of the #undef  directive. In order to undefined a macro that has been earlier #defined, the directive,

#undef macro template

Can be used. Thus the statement,

#undef  PENTIUM

Would cause the definition of PENTIUM to be removed from the system. All subsequent #ifdef  PENTIUM  statements would evaluates to false. In practice, seldom are you required to undefined a macro, but for some reason if you are required to, then you know that there is something to fall back upon.

#pragma Directives: This directives is another special-purpose directive that you can use to turn on or off certain features. Pragmas vary from one compiler to another. There are certain pragmas available with Microsoft C compiler that deal with formatting source listings and placing comments in the objects file generated by the compiler. Turbo C/C++ compiler has got  a pragma that allows you to suppress warnings generated by the compiler. Some of these programs are discussed below:

  • #pragma startup and #pragma exit.
  • #pragma warn.

Recommended Posts

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *