1 of 13

1

01/11/2023

Preprocessor

2 of 13

Introduction

    • Preprocessing occurs before compilation.
      • Including other files
      • Defining symbolic constants
      • Defining macros
      • Conditional compilation
      • Conditional execution of preprocessing directives.
    • All preprocessing directives begin with #
    • We have already seen something about #include and #define

2

01/11/2023

3 of 13

Continuing with #define : macros

    • #define PI 3.14159
    • The above one defines a symbolic constant called PI. In the program PI is replaced with 3.14159
    • This is simple replacement of one text by other text in the source (that is the C program).
    • One can specify symbolic constants with arguments also. This is normally called as macros.

3

01/11/2023

4 of 13

macros

    • #define SQR(x) x * x

    • Let the statement be

j = SQR(5);

    • Here this is replaced by

j = 5 * 5 ;

    • That is, SQR(5) is replaced by 5 * 5

4

01/11/2023

5 of 13

macros

    • #define SQR(x) x * x
    • This could create some problems !

    • Suppose we wrote a statement like

j = SQR(3+4);

    • This is replaced by

j = 3 + 4 * 3 + 4;

    • So value of j will be 19 not 49.
    • To overcome this problem one should use braces around the arguments.

5

01/11/2023

6 of 13

    • #define SQR(x) (x) * (x)

    • j = SQR(3+4);
    • j = (3+4) * (3+4);

    • It is still better to use

#define SQR(x) ((x) * (x))

6

01/11/2023

7 of 13

#undef

    • Symbolic constants and macros can be discarded by using the #undef preprocessor directive.
    • Eg:

#define PI 3.14

….

….

#undef PI

    • The scope of a symbolic constant or macro is from its definition until it is undefined or until end of the program.

7

01/11/2023

8 of 13

Some functions are actually macros

    • Functions are sometimes defined as macros using other standard functions.
    • #define getchar( ) getc( stdin )
    • The above macro is normally kept in stdio.h

8

01/11/2023

9 of 13

Conditional compilation

    • Conditional compilation enables the programmer to control the execution of preprocessor directives and the compilation of program code.
      • #if defined( PI )

area = PI * r * r;

#endif

    • The expression defined(PI) evaluates to 1 if PI is #defined 0 otherwise.
    • Similarly !defined(…) is negation of the above.
    • Every #if ends with #endif

9

01/11/2023

10 of 13

Conditional compilation

10

01/11/2023

11 of 13

Conditional compilation

    • Directives #ifdef and #ifndef are shorthand for #if defined and #if !defined
    • #elif is for else if
    • #else is for else

    • #ifdef PI

area = PI * r * r;

#else

area = 3.14 * r * r;

#endif

11

01/11/2023

12 of 13

12

01/11/2023

13 of 13

Commenting out

    • If you want to prevent a code from compilation then do
      • #if 0

code to be prevented

#endif

    • Just by removing #if 0 and #endif the code is compiled.
    • This is a good way than commenting out. Remember comments cannot be nested.

13

01/11/2023