Preprocessor Directives in C – Part 2

Read: Preprocessor Directives in C – Part 1

In the last tutorial I told you about some preprocessor directives which are macros and file inclusion. Two more preprocessor directives are left which I will complete in this tutorial.

So today I will tell you about the two preprocessor directives in C language which are

  • Conditional compilation
  • Miscellaneous directives

Conditional Compilation

The usage of this preprocessor directive is similar to its name. It is often used to exclude some set of statements through the process of compilation. One question which will hit on your mind.

Why should I write those statements which I don’t want to compile?

  • Well programmers often write programs for some clients. Suppose at one stage client demands the older version of program which you have deleted. Then at those cases it is used very widely. So it is often used to omit the working of certain functions easily without touching the code. It is safest way to remove certain code from the program.
  • It is also used to test the working of program. While writing complex C programs it is quite often that the C program gives wrong results at last moment. So we can use conditional compilation to rectify errors by using hit and trial method.
  • Portability is one of the main feature which is quite famous these days. With the use of conditional compilation we can also make the program portable.

Now lets take one example to explain the conditional compilation.

#ifdef MACRONAME
   Statement 1;
   Statement 2;
   And so on…
#endif

It means the statements 1, 2 and so on will only work when a macro is defined in the program with MACRONAME. You can also use #ifndef, it is just opposite of #ifdef. Here #else can be used to show else part of #ifdef or #ifndef. #endif shows the end of conditional compilation preprocessor directives.

Output

Preprocessor Directives in C - Part 2

Explanation

  • As you can see, the second printf() does not work. Its because we have not defined any macro with WORK.
  • To make the second printf() work, I just have to add #define WORK before main() function. After that it will also print the message inside second printf().

Miscellaneous Directives

The two preprocessor directives that are not commonly used falls inside the category of miscellaneous directives.

1. #undef
2. #pragma

#undef

This preprocessor directive is generally used to undefine certain macros at some stage. It is not commonly used but we can undefined any macros by using #undef followed by the macros name.

#pragma

This preprocessor directive is generally used in three ways which are 

a. #pragma startup
b. #pragma exit
c. #pragma warn

#pragma startup: This is used to call some function right from the start up (before executing main() function). We can call any function by using this preprocessor directive.

Syntax: #pragma startup funcall

In the above code “funcall” function will be called from the start up of the program.

#pragma exit: It is a counter part of start up. It is used to call some function when program ends.

Syntax: #pragma exit funcall

In the above code “funcall” function is called at the exit of the program.

#pragma warn: This preprocessor directive is used very rarely. It is generally used to supress a specific warning inside a program.

Leave a Comment

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