C Preprocessors

The preprocessor is a program invoked by the compiler that modifies the source code before the actual composition takes place. This tutorial describes C Preprocessor Directives.



To use any preprocessor directives, first, you have to prefix them with the # (hash) symbol

The following section lists all preprocessor directives:

Category Directive Description
Macro substitution division #include File includes.
#define
#undif
Macro define. Macro undefine.
#ifdef
#ifndef
If Macro defined. If Macro not defined.
File inclusion division #if
#elif
#else
#endif
If, Else if, Else, 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

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

#ifndef LIMIT
   #define LIMIT 50
#endif

It tells the compiler to define LIMIT only if it isn't already defined.

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

It means the compiler processes the statements enclosed if the LIMIT is defined.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Keep W3schools Growing with Your Support!
❤️ Support W3schools