11- C Preprocessors

This chapter will introduce the preprocessors in C language. You must be thinking preprocessors is going to be a complex structure, then it is not so. Preprocessors are friends which make programming more flexible and portable. The reason is it allows to incorporate modifications automatically. These preprocessor commands are also known as Directives. Preprocessor is also known as macroprocessor program. Let us once look at the flow of execution then it will become more clear to you.

Various stages of compilation

                                    Figure Various stages of compilation

In the above diagram, you could see the flow of execution. Every C program has to pass through compiler to be converted into object program which is going to be executed. Macroprocessor programs are the responsible for processing the source programs before they are passed to compiler. Please do not get confused by the word source program. Source program is the program which you create in the editor. Macroprocessor program is a bunch of statements called preprocessor directives which are executed before source program is passed to compiler. You have already used preprocessor directive without its knowledge. These statements must be declared before the main() program. There are three main categories of preprocessor directives and they are as follows:

  • Macro expansion directives
  • Directives for file inclusion
  • Directives for conditional compilation

11.1 Macro expansion / substitution

The #define is known as a macro definition statement. This is an operation of substituting an identifier by a constant or symbol. The format is as follows:

C #Define

Explanation: Here,

  • #define is a directive
  • valid_identifier is a valid identifier according to standard in C language
  • c_onstant can be constant, symbolic constant or expression

The identifier is usually written in C language. Make sure that you do not leave any space in between # and define keyword. There is no semi-colon in the end of this declarations. There should be a space left between #define and valid_identifier as well as valid_identifier and c_onstant respectively.

            As soon as you compile the program, compiler looks for C preprocessor directive #define. When it finds #define statement it searches the entire program for the templates for macro definition and as soon as it finds it it replaces it with the valid macro expansion. Macro template is valid_identifier and macro expansion is c_onstant respectively. It is useful when you have to use one constant more than once in same program. With one directive all the values for that value is going to be altered. This increases the readability of program. Let us consider the following example which will show the usage of macro substitution.

/* Program to illustrate the macro substitution */

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define PIE 3.14

void main()
  float radius, area;
  printf("Let us calculate area of a circle\n");
  printf("Please enter the radius of circle\n");
  scanf("%f", &radius);
  area=PIE * radius * radius ;
  printf("Area of circle =%f\n", area);

C Macro substitution example

                                      Figure - C Program example for macro substituion

After compiling program looks similar to the following snapshot:

   Compiled output of C macro substitution example

                                       Figure  - Compiled output of C example program for macro substitution

 Output of C example of Macro substitution

11.2 Directives for File inclusion

As the name suggests this lets you include a file in the program. All contents of filename is inserted in the source program. The file which is going to be included does exists. When you have very large programs for example a weather forecasting program which includes lot of calculations, it is best to sub-divide large program into smaller valid sub-programs. These sub-programs can be included in the beginning of main program. There are lot of programs which are mandatory to be included in all source programs. These included files do have a header file extension which is “.h”. We use #include statement for this purpose. There are two ways of doing this:

  • #include <filename.h>
  • #include “filename.h”

There is a minor line of difference between these two types. This #include <filename.h> declaration is going to look for filename.h file in the list of standard directories only which are particularly specified. As far as, #include “filename.h” is going to find file in current directory followed by list of directories mentioned in specified directories listed.

11.3 Directives for conditional compilation

The statements #ifdef and #endif are the preprocessing commands which when inserted can skip over a part of source program. You must be thinking why to have such a facility? The answer is for example, you have a business client who would want to change the execution sequence in a different way as well as he may again want you to show the previous way of execution. In this type of situations, we need such alternatives. This is also helpful when you need to execute same program in different computer architectures and this is also possible by this method. The format is as follows:

                        Format of C conditional compilation

              Figure - format of C conditional compilation

Explanation: Here,

Statement 1, Statement 2 will be compiled only if ALRIGHT macro is defined,  otherwise they will not be.  After sometime if you want them to be always executed then we can simply delete #ifdef and #endif keywords.

There is an alternative for this flow of execution. This alternative is similar to if-else control structure and that is #ifdef-#else-#endif. The format will be similar to following example,

          format of #ifdef-#else-#endif

Explanation: Here,

Code for system_type_1 will be executed only when ALRIGHT macro is defined otherwise it will execute Code for system_type_2 and Code common for both systems respectively. 

There is another similar directive which is #ifndef and it is if not defined. This is a directive which is opposite to #ifdef. Let us write the same code which is defined above to make things more clear.


Explanation: Here,

 Code for system_type_1 will be executed when ALRIGHT macro is not defined  otherwise it will execute Code for system_type_2 and Code common for both  systems respectively.

        Format of C #if-#else-#endif 

   Figure - format of C #if-#else-#endif

Explanation: Here,

Statement 1, Statement 2, Statement 3 will be executed if expression results to value    which is less than on equal to 2 otherwise Statement 4, Statement 5 will be executed  respectively.

11.4 Other directives

We will also deal with two more directives and they #undef and #pragma respectively.

#undef directive deletes the defined directives. This would erase all the definition related to the defined macro template and all #ifdef directives will evaluate to zero. Format is as follows:

     Format of C Undef Directive

            Figure - C format for undef directive

Explanation: Here,

  • #undef is the directive
  • defined_valid_identifier is the macro template
  • c_onstant is macro expansion

#pragma is a special purpose directive. This turns on/off certain features of Microsoft C Compiler. For example, Turbo C allows us to write assembly language statements in C source program. Folks with this we conclude this chapter. Next chapter is dedicated to Input/Output statements in C . Thank you.

Like us on Facebook