C Preprocessors

By | February 25, 2024

Prerequisite – C language
C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. Preprocessors are programs that process our source code before compilation. C Preprocessor is just a text substitution tool that instructs compiler to do required pre-processing before actual compilation.

All preprocessor commands begin with a hash symbol (#). There are 4 main types of preprocessor directives:

  1. Macros
  2. File Inclusion
  3. Conditional Compilation
  4. Other directives

Let us learn in brief.

1. Macros :

The ‘#define’ directive is used to define a macro. Example –

// macro definition 
#define RANGE 10  

Note that there is no semi-colon(‘;’) at the end of macro definition. Macro definitions do not need a semi-colon to end.

We can also pass arguments to macros which works as function:

// macro with parameter 
#define AREA(l, b) (l * b)  

2. File Inclusion :

These tells the compiler to include a file in the source code program.

  • Header File or Standard files –
    These are predefined.

    #include< file_name > 
  • User Defined files –
    We use these for large file.

    #include"filename" 

3. Conditional Compilation :

These help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ifdef‘ and ‘endif‘.

Syntax :

#ifdef macro_name
    statement1;
    statement2;
    statement3;
    .
    .
    .
    statementN;
#endif

4. Other directives :

  • #undef (undefine) Directive –
    #undef RANGE
  • #pragma Directive –
    This directive is a special purpose directive and is used to turn on or off some features.
    #pragma startup and #pragma exit : to specify the functions that are needed to run before program startup( before the control passes to main().
    #pragma warn Directive : to hide the warning message which are displayed during compilation.

Functions of C preprocessor:
The C preprocessor performs the following functions:

1. Includes header files
2. Expands macros
3. Performs conditional compilation
4. Controls lines
5. Inserts files
6. Specifies compile-time error messages
7. Applies machine-specific rules to sections of code
8. Transforms programs before compilation  



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.