The C Preprocessor Directives

The job of the C Preprocessor is to process the source code before it is passed to the compiler. The preprocessor command is also known as directive.

The C Preprocessor

The C Program is often called as source code. Before the program is compiled, the source code goes through one process called preprocessor.

The preprocessor gets the source code (pr.C file) as input and creates expanded source code (pr.I file). This expanded source code is then passed to the compiler for compilation.

Following are the preprocessor directives:

  1. Macro expansion
  2. File inclusion
  3. Conditional Compilation
  4. Miscellaneous directives

Lets discuss these preprocessor directive one by one.

Macro expansion
Let us understand the macro expansion using an example.

Example:

If we run the above example, we will get the output as “The value of a is 10”.
In the above example, we have used macro definition. As we learnt before, before compilation process the Source code goes through preprocessing process. During preprocessing, the program is scanned from top to bottom. Every occurrence of TEN in the source code is replaced by 10.

Thus, after preprocessing the program looks like give below,

The preprocessor directive ‘#define’ can also be used to define operators as shown below.
#define AND &&
#define OR ||

After the definition of these operators we can use AND instead of && and OR instead of || within the program. This increases the readability of the program.

The above explained macro is called as ‘Simple macro’. There is another form of macro called ‘Macros with Arguments’.
Let us understand ‘Macros with Arguments’ with the help of an example.

Example:

If we run the above example, we will get the output as “AREA = 314.000000”.

In the above example, we have used macros with arguments. As we learnt, before compilation process the Source code goes through preprocessing process. During preprocessing, the program is scanned from top to bottom. Every occurrence of AREA(10) in the source code is replaced by (3.14 * 10 * 10).
Thus, after preprocessing the program looks like give below.

Macros can be split into multiple lines using back slash (‘\’) as shown below.

File Inclusion
File inclusion directive causes one file to be included in another. We have already used file inclusion directive before. The preprocessor command for file inclusion looks like this:

#include “File name”
OR
#include

E.g. #include

The above statement in C will include the file “stdio.h” in the program.

If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source directory.

Conditional Compilation
The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.
If the macroname has been #defined, the block of code will be processed as usual.

If we use #if, #ifdef, #ifndef, #else, #elif or #endif directives, it will not be processed as usual. Using these directives we can have compiler to skip over part of a source code.

Let us understand the conditional compilation directives using an example.

Example:

In the above example, statement 1 and statement 2 would be processed only if we define macro ‘YES’ else it won’t be processed.
As seen in the above program, macro ‘YES’ has not been defined. Hence, only statement 3 would be processed.

Example:

In the above example, statement 1, Statement 2 and Statement 3 would be processed as we have defined macro ‘YES’.

Example:

The above example can be read as follows:
If macro ‘YES’ is defined then
Process Statement 1 and Statement 2
Else
Process Statement 3
The above example will process Statement 1 and Statement 2.

#ifndef (if not defined) is exactly opposite to #ifdef.
Example:

The above example can be read as follows:
If macro ‘YES’ is not defined then
Process Statement 1 and Statement 2
Else
Process Statement 3
The above example will process Statement 3.

The #if directive can be used to test whether an expression evaluates to non zero or not. If the expression evaluates to non zero then the subsequent lines upto #else, #elif or #endif are compiled, otherwise they are skipped.

Example:

In the above program, if VALUE == 5 returns true then Statement 1 would be processed, else if VALUE <= 10 returns true then Statement 2 would be processed else Statement 3 would be processed.

Miscellaneous directive
#undef directive causes a defined name to become undefined. In order to undefined the macro that has been defined earlier, the directive, #undef macro name can be used. Thus, #undef VALUE would cause the definition of VALUE to be removed from the system.
#pragma directive is another very special and useful directive. This directive is used to specify diverse options to the compiler. These options are specific for the platform and the compiler you use.
If the compiler does not support a specific argument for #pragma, it is ignored – no error is generated. #pragma startup and #pragma exit are most commonly used pragma directives.
#pragma startup allows you to specify a particular function that are called upon program startup (before the execution of main()).
#pragma exit allows you to specify a function that can be called just before the program terminates.

Example:
void display1();

Output:

Please provide your valuable comments about this article or anything related to this website. It would really help me in improving the website.

You may also like...

15 Responses

  1. Loveleen Sharma says:

    how to create a header file in c………???

  2. LearnCOnline says:

    Steps to create header file in Turbo C.
    1) Create a file named yourfilename.h in the folder C:/TC/INCLUDE if you have installed Turbo C directly in

  3. Anonymous says:

    This lesson is short and valuable. Thanks for writing it.

  4. Anonymous says:

    Thanks… I got the hang of this so easily… 🙂

  5. Anonymous says:

    this clears my doubt

  6. happy shravan says:

    This site was awesome bro ..

  7. Sunil Shahu says:

    it was helpful..!!thanx..!!

  8. Anonymous says:

    In the last example, if we do not use pragma, will the sub functions still run.

  9. macp says:

    thanks. Its useful for revision

  10. Anonymous says:

    this site is very helpful for me.thanks for crating this site.please also tell about input,output files please reply at your earliest possible

  11. Anonymous says:

    why we use # before every statement not another special character

  12. Anonymous says:

    tell me why we use # before any header file not any special character

  13. Anonymous says:

    pls include #error,stringize # and token pasting ##

  14. In the area example using macros with passing arguments….there is a mistake i guess..
    When you pass an arguments to macro, without a receiving argument at macro definition, it becomes an error… so the macro definition can be changed as follows:
    #define AREA(a) 3.14 * a * a..
    Correct me if I am wrong..
    Thank you..

Leave a Reply

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