Preprocessor Directives in C – Part 1

A program goes from several stages before getting executed. The program we write is converted into source code by using text editor which is stored with extension .C. After that it is converted to expanded source code by using preprocessor which is stored with extension .I. Now this expanded source code is converted to object code by using compiler which is stored with extension .OBJ. After that it is converted into executable code by using Linker and stored with extension .EXE.

Preprocessor Directives in C - Part 1

Preprocessor Directives in C

Preprocessor directive is a text substitution tool that instruct the compiler to pre-processor our program before its actual compilation. Each C preprocessor starts with # symbol. Generally they are defined at the beginning of the program just after the header files. But you can define them anywhere in the program.

Types of Preprocessor Directives in C

There are four types of preprocessor directives which are given below.

1. Macros
2. File inclusion
3. Conditional compilation
4. Miscellaneous directives

Macros

Macros are generally used to give a common name to the values which are used many times in a program. Or we can say that, macros are used to define symbolic constants. Consider below program to understand it.


Output

Macros

Points to remember

  • A macros always start with #define. In our program #define PI 3.14 is the macros definition. Wherever PI is encountered in the program it is replaced with 3.14. 
  • In the compilation of the program all the macros definition replaced with the values which are used with #define. It is done in the process when source code is converted into expanded source code.
  • Do not add semicolon at the end.
  • Generally macros are written in single line. If you want to write it in multiple lines then you have to use macro continuation operator i.e. . An example for this is given below.

Examples


#define OR ||
#define CHECK (a>10 && a<20)
#define MSG
printf(“It is working fine”)

Macros with arguments

Macros with arguments are similar to functions with arguments. Consider the below program to understand its working.

Output

Macros with arguments

Points to remember

  • In the above program I have used macros with argument with the statement #define PERI(x) (2*3.14*x)
  • Be careful while defining macros with arguments. In above example there must be no space between PERI and (x). The body of the macro should be placed in parentheses.
  • Whenever the macro calling is done in our program, its calling statement in replaced by its body.
  • They are fast as compared to functions because control do not goes to macro definition, the whole body comes at the place where calling is done.

File Inclusion

As its name suggests this preprocessor directive is used to include all the content of file to be included in the source code. Till now we have used preprocessor directives like #include<stdio.h> which includes all the contents from the header file stdio.h. These are predefined header files, you can also make your own and then include it in your program.

Examples


#include<math.h>
#include<stdlib.h>

Why it is used?

Its certainly a good question. It is not a good programming practice to write full program into a single file. While writing a very complex program it is recommend to break the program into files and include them in the beginning. Sometimes the macros of the program is also quite large which can only be stored in some files. So it is basically used for better management of the C program.

Leave a Reply

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