The Preprocessor

#include

#define

If you have ever gone through C Programing Language, then it is sure that you have seen that two things mentioned above. They are the preprocessor.

The C programming language has too many un unique features. The preprocessor is one of those unique features of C programing language. The preprocessor in C is not a part of the compiler. It is a totally separate thing in the process of compilation. It gives many tools that are not present in many high-level languages. The preprocessor works in the mid of the source code and compiler.

Step 1
Step 2
Step 3
Source Code
Preprocessor
Compiler

Preprocessor means a program that processes the source code before the compiler comes to it. Its directives are placed in the source program before the mainline. Preprocessor directives follow special syntax rules which are not similar to the normal C programing language syntax. Its syntax begins with the symbol #. It doesn’t need a semicolon after the line. For instance, #inlude <stdio.h>  is one of those. Here the “#include” is the preprocessor. The preprocessor helps the coder to make his program easy to read, easy to modify, more efficient, and for so many purposes.

the preprocessor in c

Before starting, let us look at some directives and their functions:

SL No.
Directives
Functions
01
#include
Adds a particular header from some another file.
02
#define
Defines a macro substitution
03
#undef
Undefines a macro substitution
04
#ifdef
Return to true if the macro is defined
05
#endif
Denotes the ending of #if
06
#if
Test a compile-time condition if it is true or not
07
#esle
Denotes Specifies alternatives when the #if test fails.
08
#pragma
Issues special orders/commands to the compiler.
09
#else
Alternative directives for #if directive
10
#elif
#if and #else both in one directive
11
#error
Prints error
12
#idndef
Tests if a macro is defined or not. 

These directives can be divided into 4 parts.
  1. #difine is macro syntax.
  2. #include is header file inclusion
  3. #ifdef, #ifndef, #if, #endif, #else are conditional compilation
  4. #undef, #pragma etc are other directives. 

preprocessor flow chart

Some Preprocessor Examples

1. #include <stdio.h>

These directives tell the CPP to add stdio.h from libraries. It also adds the text to the present source file. #include <file_name>  command CPP to add that file from system library.

2. #define MAX_ARRAY_LENGTH 15

It means that CPP will replace instances of MAX_ARRAY_LENGTH with 15.
All those directives are part of 3 categories.
  1. Macro Substitution
  2. File Inclusion
  3. Compiler Control Directives
Now we can go to the deep of macro substitution.

Macro Substitution

Macro substitution is a process that uses an identifier in a program that is replaced by a predefined string composed of one or more tokens.

Predefined Macros

Some predefined macros and their function are being mentioned below.
SL No
Macro
Description
01
__FILE__
This macro contains the present filename is a string literal
02
__TIME__
The current time as a char literal of Hour, Minute, and Seconds as “HH:MM:SS” form.
03
__DATE__
This is in MM:DD:YYYY format
04
__LINE__
It contains the current line numb as decimal cons
05
__STDC__
Define as 1 when the compiler complies with ANSI Standard

Most common forms of macro substitution are:
  • Simple macro
  • Argumented macro
  • Nested macro

1. Simple Macro Substitution

In Simple macro substitution, a macro is simply replaced. This replacement occurs by a strig. The general form of simple macro substitution is:
#define identifier string

e.g of constants are as follows:
#define
COUNT
70
#define
CAPITAL
DHAKA
#define
PI
3.1416
#define
FALSE
0

Things to remember is, all the identifiers or macros are to in capital letters to mark those as symbolic constant.

Example in brief

Look at the macro below

#define TECHKIB 71

This will replace all the TECHKIB with 71, from the start of the line of definition to the end.
[Note: A macro inside a string doesn’t get replaced]

Look at the following line:

printf (“TECHKIB = %lf”, TECHKIB);
this line will change to the line below
printf(“TECHKIB = %lf”, 71); 

Some more examples of simple macro substitution

#define AREA 3*7
#define SIZE sizeof(int)*8  

Anytime we use the expressions for replacement, should be taken to prevent an unexpected order pf evaluating process. Have a look at the equation below:
ratio = R/S;
where
#define R 71-49
#define S 75+02
Therefore, the result of the preprocessor’s substitution for R and S is:
ratio = 71-49/75+02;
This is totally different than the equation ratio = (71-49)/(75+02)

Some more definitions:

#define
AND
&&
#define
OR
| |
#define
NOT_EQUAL
!=
#define
START
main(){
#define
END
}
#define
BLANK_LINE
printf(“\n”);
#define
INCREMENT
++
#define
EQUALS
==

2. Argumented Macro Substitution:

Because of the function, a macro can have arguments. The preprocessor that permits us to define the more complex form of replacements it takes from the following form:
#define identifier(f1, f2, f3 … … … fn) string
The identifiers f1, f2, f3 … … … fn is the formal macro arguments that are analogous to the formal arguments in a function definition.

An example is #define SQUARE(a) (a*a*a)
And then, if the statement comes in the program as area = SQUARE(side);
The preprocessor would expand the statement to area = (side*side);

Some more frequently used definitions are:

#define
MAX(a,b)
(((a)>(b)) ? (a) : (b))
#define
MIN(a,b)
(((a)<(b)) ? (a) : (b))
#define
STREQ(x1,x2)
(strcmp((s1,) (s2) == 0)

3. Nested Macro Substitution:

This is the process to use a macro in the definition of another macro. Look at the definition below:
((SQUARE(g)*(g)*(SQUARE(g)*(g)))
As SQUARE(g) is a macro till this position of the program, it will be expanded to
( ( ((g)*(g))*(g))*( ((g)*(g))*(g)) )
Which is finally evaluated as g^6 

Look at the examples below:

#define
A
7
#define
Z
A+49
#define
CUBE(p)
SQUARE (x)*(x))
#define
SIXTH(p)
(CUBE(p)*CUBE(p))

How to undefine a macro?
Macro defined with #define directive should be undefined with #undef directive.
#undef identifier
Below is the example of defining a macro and undefining a macro.

Defining


#include<stdio.h>
#include<conio.h>
#define PI 3.1416
#void main( )

{
    int r=5, area;
    area = PI*r*r;
    printf(Area is %d, area);
}


Undefining

#include<stdio.h>
#include<conio.h>
#define PI 3.1416
#void main( )

{

    printf (PI=%d, PI);

#undef Pi

    printf (PI=%d, PI);

}


Md. Rabiul Mollah

Okay! So here I'm Md. Rabiul Mollah from Pathgriho Network. I'm currently a student of B.Sc in Textile Engineering Management at Bangladesh University of Textiles. facebook instagram github twitter linkedin

Post a Comment (0)
Previous Post Next Post