C Preprocessors

The preprocessor is a program invoked by the compiler that modifies the source code before the actual composition takes place.

To use any preprocessor directives, first, we have to prefix them with pound symbol #.

The following section lists all preprocessor directives:

CategoryDirectiveDescription
Macro substitution division#includeFile include
#define
#undif
Macro define, Macro undefine
#ifdef
#ifndef
If macro defined, If macro not defined
File inclusion division#if
#elif
#else
#endif
If, Else, ifElse, End if
Compiler control division#line
#error
#pragma
Set line number, Abort compilation, Set compiler option

C Preprocessors Examples

Syntax:
#include <stdio.h>

/* #define macro_name character_sequence */
#define LIMIT 10

int main()
{
    int counter;
    for(counter =1; counter <=LIMIT; counter++)
    {
        printf("%d\n",counter);
    }
return 0;
}

In the above example for loop will run ten times.

#include <stdio.h>
#include "header.h"

#include <stdio.h> tell the compiler to add stdio.h file from System Libraries to the current source file, and #include "header.h" tells the compiler to get header.h from the local directory.

#undef  LIMIT
#define  LIMIT 20

This tells the compiler to undefine existing LIMIT and set it as 20.

#ifndef LIMIT
   #define LIMIT 50
#endif

This tells the compiler to define LIMIT, only if LIMIT isn't already defined.

#ifdef LIMIT
   /* Your statements here */#endif

This tells the compiler to do the process the statements enclosed if LIMIT is defined.