Preprocessor in C

'Preprocessor' is define as a code which is process before compilation.Preprocessor programs gives preprocessor directives which tell the compiler to preprocess the source code before compiling.All the Preprocessor start with (#) symbole.It is not a part of compiler.The following are the preprocessor directives.

S.No Directive Description
1 #define Substitutes a preprocessor macro.
2 #undef Undefines a micros
3 #include Include a file source in a program.
4 #if Check the compile time condition.
5 #else It is alternative for (#if).
6 #warning It allows generating a level one warning from a specific location in your code.
7 #ifdef Test for micro defination.
8 #endif Spacify the end of #if.
9 #error Print the error message in output screen.
10 #pragma Issues special commands to the compiler, using a standardized method.

Preprocessor directive are mainly divided into 4 parts:

  • Micros.
  • Conditional Compilation.
  • File Inclusion.
  • Other directives.

Micros

It is a piece of small code in a program which is given some name when the name comes by the compiler the compiler replaces the name with the actual piece of code.

#define min(a, b) ( (a) < (b) ? (a) : (b) )
int foo(int x, int y)
{
    return max (a, b);
}
//After replacing their name
int f(int x, int y)
{
    return ( (a) < (b) ? (a) : (b) );
}

Some Predefined micros

S.No Micros Description
1 __FILE__ It gives the file name of the current source file (a string literal)
2 __LINE__ for the current line number.
3 __DATE__ for the compilation date.
4 __TIME__ Check the compile time condition.

Conditional Compilation

Conditional compilation as the name implies that the code is compiled if certain condition hold true,the following are the part of conditional compilation.

S.No Preprocessor
1 #if,#else,#enfif
2 #if,#endif
3 #if,#elif,#else,#endif
4 #ifndif,#endif

File Inclusion

It is used to include user define header file inside C Program and start with (#include).Syntax of the file inclusion is

#include<name of file>
Example
#include<stdio.h>

In this file "stdio.h" in which scanf and printf function are predefine.