C Programming language-C Preprocessors

C Preprocessors

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

To use any preprocessor, we have to prefix them with a pound symbol (#).

The following are the preprocessor directives:


Category 
Directives
Description
Macro substitution division
#include 
Include the File

#define

#undef

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 the line number 

Aborting the compilation

set compiler option


Example 1:

#include<stdio.h>
#define MAX 5
int main()
{
    int i;
    for(i = 1; i <= MAX; i++)
    {
        printf("Hello %d", i);
        printf("\n");
    }
}

Output

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
In the above example, the loop will run 5 times.
#include tells the compiler to add stdio.h from System Libraries to the current source file
#define MAX 5, tells the compiler to set the value 5 to it.

Example 2:

#undef MAX
#define MAX 10
In the above example, undefined existing MAX and set it to 10.

Example 3:

#ifndef MAX
	#define MAX 100
#endif

The compiler will define MAX only when it is not defined before.