The Preprocessor in C Language – C Preprocessor Directives

What Is C Preprocessor?

A preprocessor is a program that processes Source Code before it passes through the compiler for compilation.

There are some preprocessor commands and directives that are used to instruct Preprocessor and mostly placed at the starting of the C source code.

To include the various instructions to the compiler in the C source code, some directives are used called as Preprocessor Directives.

Preprocessor expands the scope of the programming environment.

Preprocessor commands are executed before the compiler compiles the source code.

These commands will change the original code according to the operating environment and adds the code that will be required by calls to library functions.C PREPROCESSOR DIRECTIVES

There are 2 main uses of preprocessor directives :

  1. To include external files such as Header files(stdio.h , math.h etc).
  2. To define macros.

Preprocessor commands begins with a symbol # as directives. example, a standard input-output header file is written as :
#include
It is a command which tells the preprocessor to include this external header file as part of the program to be compiled.
Each preprocessor directive must be on its separate line,
Example
Wrong:

#include<stdio.h> #include<math.h>

Correct:

#include<stdio.h>

#include<math.h>

To maintain the portability of C programming language in different architectures or compilers,the concept of Preprocessor is introduced.

All the preprocessor processed before the staring of actual compilation and create an intermediate file.

In the intermediate file all preprocessor statement/commands are replaced by corresponding C code.

During the process of compilation that intermediate file is deleted by the compiler automatically.

Advantages of Preprocessor in C

  • It improves the readability of C program.
  • It makes the C program easy to maintain.
  • It makes the C program more portable.

C Preprocessor Directives

In ‘C’ all the preprocessor directives start with # character except define() operator.

Each directive have different task which executes just before the actual execution of C program which makes the program more portable.

These directives are used only to instruct compilers.

FILE INCLUSIVE DIRECTIVE  #include
MACRO SUBSTITUTION DIRECTIVE  #define
UNDEFINE SYMBOL DIRECTIVE  #undef
CONDITIONAL DIRECTIVE # if  #elif  #else
#endif   #ifdef   #ifndef
MISCELLANEOUS DIRECTIVE  #pragma   #line   #error
OPERATOR IN PREPROCESSOR  #,##,  define()

Spaces or tabs are permitted between the # and directive name (such as include, define etc) but not escape characters or any other symbols are permitted. The preprocessor removes white space and concatenates the # and directive name together.

Leave a Reply

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