Preprocessors in C

What are Preprocessors in C? Definition – A preprocessor in C is nothing but a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. And the command used in the preprocessor in C are called preprocessor directives and they begin with a ‘#’ symbol.

preprocessor in c

Preprocessors in C

And there are different directives available preprocessors in C. Here are as follows:

  1. Macros
  2. Header file inclusion
  3. Conditional compilation
  4. Other directives

Macros in C

The macro defines constant value and can be of any of the basic data types.

 Syntax: #define    

And Macros are divided into 2 types. They are

  • Function like macros
  • Object like macros

1. Function like Macros

A function like macros looks like a function call.

Example

 #define MIN(a,b)((a)<(b)?(a):(b))

 2. Object like macros

An object like macros is an identifier, which is replaced by value. It is widely used to represent numeric constants.

Example

#define PI 3.1415
Category Directive Description
Macro substitution division #include
#define
#undif
#ifdef
#ifndef
The file includes Macro define,
Macro undefine
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

Example

#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;           
}

Output
value of height: 100
value of a number: 3.140000
value of letter: A
value of letter_sequence : A
value of backslash_char:?

Example 2: program for #IFDEF, #ELSE and #ENDIF in C

#include <stdio.h>               
#define RAJU 100                 
int main() {                   
#ifdef RAJU                   
printf("RAJU is defined. So, this line will be added in " \"this C file\n");                 
#else               
printf("RAJU is not defined\n");               
#endif             
return 0;  }

Output: RAJU is defined. So, this line will be added in this C file

Example 3: Example program for PRAGMA in C language.

#include <stdio.h>                
void function1( );#pragma startup function1                
void function2( );#pragma exit function2          
int main( )  {                 
printf("\n Now we are in main function" );                 
return 0; }             
void function1( ) {               
printf("\nFunction1 is called before main function call");     
}         
void function2( ) {             
printf ("\nFunction2 is called just before end of "\"main function" );" 
}

Output
Function1 is called before the main function call
Now we are in the main function
Function2 is called just before the end of the main function

Predefined Macros

ANSI C defines a number of macros. Although each one is available for use in programming, the predefined macros should not be directly modified.

Macro Description
__DATE__ The current date as a character literal in “MMM DD YYYY” format
__TIME__ The current time as a character literal in “HH:MM:SS” format.
__FILE__ This contains the current filename as a string literal
__LINE__ This contains the current line number as a decimal constant
_STDC_ Defined as 1 when the compiler complies with the ANSI standard