Preprocessor in c


  • One of the most important features of C language is to offer preprocessor directives.
  • The preprocessor directives are always preferably initialized at the beginning of the program before the main() function.
  • It begins with a # (hash) symbol. It can be placed anywhere, but quite often it is declared at the beginning before the main() function or any particular function. In traditional C, # (hash) must begin at the first column.
  • The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process.
  • In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We'll refer to the C Preprocessor as CPP.
  • All Preprocessor commands beging with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column.

     

    Directive Description
    #define Substitutes a preprocessor macro.
    #include Inserts a particular header from another file.
    #undef Undefines a preprocessor macro.
    #ifdef Returns true if this macro is defined.
    #ifndef Returns true if this macro is not defined.
    #if Tests if a compile time condition is true.
    #else The alternative for #if.
    #elif #else and #if in one statement.
    #endif Ends preprocessor conditional.
    #error Prints error message on stderr.
    #pragma Issues special commands to the compiler, using a standardized method.

    Example:

    #include <stdio.h>
    #define PI 3.14
    int main()
    {
        int radius;
        float area;
        printf("Enter the radius=");
        scanf("%d",& radius);
        area = PI*radius*radius;
        printf("Area =%f",area);
        getch();
        return 0;
    }
    

    Output:

    Enter the rasius 5
     Area= 78.5