Pre Processor directives in C Programming

C Pre Processor directives informs the compiler to include the header file code before compiling. Pre Processor statements in program begins with the # hash followed by the directive.
Below are the directives that can be used for processing

1. #include


#include is used to include the c programming standard header files or the user defined header files the most common header files is stdio.h.

#include <stdio.h>

Below is the example to include the user defined header file "fun.h" the compiler searches the immediate directory where the user defined header file should be present.

filename : fun.h

#include <stdio.h>

int fun(int count) {
    int index;
    for ( index = 0; index < count ; index ++) {
        printf("%s\n","fun");
    }
    return 0;
}

filename : fun.c

#include "fun.h"

int main() {
    fun(5);
}


2. #define, #undef, #ifdef and #ifndef


define is used to define macro that will substitute into coding statements during the time of pre processing. Below is the example where macro EMPLOYEE_CHAR_SIZE employee name cannot exceed more than 256 characters. #define will not have semicolon at the end of the statement

#define EMPLOYEE_CHAR_SIZE 256

Second Example is related to macro function to convert US Dollar to Indian Rupees. Assuming 1 USD is 70 rupees.

#define USDTOINR(x) (x*70)

#undef is used to un define the existing defined macro if USDTOINR is defined then if dollar rate changes to 74 Indian rupees then we can use #ifdef and #undef to redefine the USDTOINR

#include <stdio.h>

#define USDTOINR(x) (x*70)

#ifdef USDTOINR
    #undef USDTOINR
    #define USDTOINR(x) (x*74)
#endif

int main() {
    printf("\n%d\n", USDTOINR(1));
    return 0;
}


#ifndef is a macro used when ever a macro is not defined, then execute some coding statements inside this if not defined pre processor directive

#include <stdio.h>

#ifndef USDTOINR
    #define USDTOINR(x) (x*74)
#endif

int main() {
    printf("\n%d\n", USDTOINR(1));
    return 0;
}


3. #if, #else, #elif and #endif


#if #else #elif and #endif are pre processor directives that can be used for conditional execution. fox example we can include libraries based on the operating system that the program is running on. As given below we can check the machine type and based on that we are printing the Operating System types

#include <stdio.h>

#if defined( sun )              
    #define OSIS "Its a Sun Solaris"
#elif defined ( _AIX )          
    #define OSIS "Its a Sun Solaris"
#elif defined( __hpux )        
    #define OSIS "Its a HP Unix"
#elif defined(i386) || defined(__i386__)     
    #define OSIS "Its a x86"
#else
    #define OSIS "Unknown"
#endif

int main() {
    printf("\n%s\n", OSIS);
    return 0;
}


4. #error


#error pre processor will not allow you to compile the code. We can use #error to exit the compilation process based on some condition as below example if we comment the line "//#undef FORMULA_SQUARE" the code passes compilation else if we un comment the compilation will fail

$ cat kk.c
#include<stdio.h>  
#define FORMULA_SQUARE(x) (x*x)

// #undef FORMULA_SQUARE

#ifndef FORMULA_SQUARE
    #error formula to calculate square is not defined  
#else  

int main() {  
    int area = 25;
    printf("%d",FORMULA_SQUARE(area));  
}  
#endif
$
$ cc kk.c
$ ./a.out
625
$
$ cat kk.c
#include<stdio.h>  
#define FORMULA_SQUARE(x) (x*x)

#undef FORMULA_SQUARE

#ifndef FORMULA_SQUARE
    #error formula to calculate square is not defined  
#else  

int main() {  
    int area = 25;
    printf("%d",FORMULA_SQUARE(area));  
}  
#endif
$
$ cc kk.c
kk.c:7:6: error: #error formula to calculate square is not defined
     #error formula to calculate square is not defined
      ^~~~~
$


5. #pragma


Pragma pre processor directive is used to give instruction to compiler to turn on or off some of the compilation process. 


#include<stdio.h>  
 
void func();  
 
#pragma startup func  
#pragma exit func  
 
void main(){  
int kk;
printf("\nI am in main");  
scanf("%d",&kk);
}  
 
void func(){  
int kk;
printf("\nI am in func");  
scanf("%d",&kk);
}  

The same will compile differently in Windows and in Linux

Windows
================================
./a.out
I am in func
21
I am in main
21

Linux GCC
================================
./a.out

I am in main
21


Most of Linux Compilers does not support pragma directive this can be only seen with Windows Operating System Compilers specially non GNU C Compilers. As given below program it will instruct the compiler to run func() function before main() function.

Post a Comment

0 Comments