Skip to main content

C PREPROCESSOR

DEF:- The C Preprocessor or cpp is the macro processor for the C and C++ programming language.It provide the ability for the inclusion of header file,macro expansion,conditional compilation(#if,#ifdef,#ifndef,#else,#elif,#endif) and line control.

NOTE:- preprocessor directive are executed before compilation.All preprocessor directive starts with the # hash-symbol.
There any many preprocessor directives in c language.In the figure there are two #if written.


1)#include:- The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error.
By the use of #include directive, we provide information to the preprocessor where to look for the header files. There are two variants to use #include directive.

(a) #include <filename> :- The #include <filename> tells the compiler to look for the directory where system header files are held. In UNIX, it is \usr\include directory.

(b) #include "filename" :- The #include "filename" tells the compiler to look in the current directory from where program is running.

2)#define:- it is used to define constant.                                                                                   For example: #define pi 3.14; //means all pi in program having or initialize the value 3.14
Example:- #include <stdio.h> 
                  #define PI 3.14   
                  main() { 
                        printf("%f",PI); 
                  }  //output is 3.14

3)#undef:- this directory is used to undefine the constant or macro which is define by #define.
Example:-  #include <stdio.h> 
                   #define PI 3.14 
                   #undef PI 
                   main() { 
                          printf("%f",PI); 
                   } //now output is given a errror becuase there is no pi initialized. 

4)#ifdef:- The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present.

Syntax:
    #ifdef MACRO 
                 //code 
    #endif 

Syntax with #else:
      #ifdef MACRO 
                  //successful code 
     #else 
                //else code 
    #endif 

Example:-
#include <stdio.h> 
#include <conio.h> 
#define NOINPUT  //define noinput
void main() { 
int a=0; 
#ifdef NOINPUT  //yes it is define
a=2; 
#else 
printf("Enter a:");  //if no input is not define then else code is running.
scanf("%d", &a); 
#endif       
printf("Value of a: %d\n", a); 
getch(); 

Output: Value of a: 2

5)#ifndef:- The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code otherwise #else code is executed, if present.

Syntax:

#ifndef MACRO 
//code 
#endif 

Syntax with #else:

#ifndef MACRO 
//successful code 
#else 
//else code 
#endif 

Example:-
#include <stdio.h> 
#include <conio.h> 
#define INPUT 
void main() { 
int a=0; 
#ifndef INPUT  //no it is define so this code not run
a=2; 
#else 
printf("Enter a:");  //this code run
scanf("%d", &a); 
#endif         //end the #if directive
printf("Value of a: %d\n", a); 
getch(); 

Output:
Enter a:5
Value of a: 5

6)#if:- The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.

Syntax:

#if expression  
//code  
#endif  

Syntax with #else:

#if expression  
//if code  
#else  
//else code  
#endif  

Syntax with #elif and #else:

#if expression  
//if code  
#elif expression  
//elif code  
#else  
//else code  
#endif  

Example:-
#include <stdio.h>  
#include <conio.h>  
#define NUMBER 0  
void main() {  
#if (NUMBER==0)  
printf("Value of Number is: %d",NUMBER);  
#endif         
getch();  
}  
Output: Value of Number is: 0

7)#else:- The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives.

Syntax:

#if expression  
//if code  
#else  
//else code  
#endif  

Syntax with #elif:

#if expression  
//if code  
#elif expression  
//elif code  
#else  
//else code  
#endif  

Example:-
#include <stdio.h>  
#include <conio.h>  
#define NUMBER 1  
void main() {  
#if NUMBER==0  
printf("Value of Number is: %d",NUMBER);  
#else  
print("Value of Number is non-zero");  
#endif         
getch();  
}  
Output: Value of Number is non-zero

8)#error:- The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found and skips further compilation process.

Example:-
#include<stdio.h>  
#ifndef __MATH_H  
#error First include then compile  
#else  
void main(){  
    float a;  
    a=sqrt(7);  
    printf("%f",a);  
}  
#endif  

Output: Compile Time Error: First include then compile

But, if you include math.h, it does not gives error.

#include<stdio.h>  
#include<math.h>  
#ifndef __MATH_H  
#error First include then compile  
#else  
void main(){  
    float a;  
    a=sqrt(7);  
    printf("%f",a);  
}  
#endif
  
Output: 2.645751

9)#pragma:- The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating-system feature.

Syntax:  #pragma token (token is identifier,variable,etc.) 

The turbo C++ compiler supports following #pragma directives.

  • #pragma argsused  
  • #pragma exit  
  • #pragma hdrfile  
  • #pragma hdrstop  
  • #pragma inline  
  • #pragma option  
  • #pragma saveregs  
  • #pragma startup  
  • #pragma warn  

#include<stdio.h>  
#include<conio.h>  
void func() ;  
#pragma startup func  
#pragma exit func  
void main(){  
printf("\nI am in main");  
getch();  
}    
void func(){  
printf("\nI am in func");  
getch();  
}
  
Output:
I am in func
I am in main
I am in func

Comments

Popular Posts

CALL BY VALUE AND REFERENCE--

A function can be called by two types 1) Call By Value:-   when an original value is not modified , is known as call by value. In call by value the actual parameter(the parameter passed to function) is copied into the formal parameter(parameter receive by function). in this the value of each variable in calling function is copied into the corresponding dummy variable of the called function. the changes made to the dummy variable have no effect on the actual variable. Example:-   2)Call By Reference:-   when an original value is modified, is known as call by reference.in this a reference is used , denoted by &(symbol). we can use either pointer or reference for call by reference in this the address of the variable is passed into the function called by actual parameter. in this the memory allocation is same for both actual and formal arguments. Example:-   

Static Memory Allocation

Statement are of two types:- Declaration:- in which we declare the variable Action:- in which for,if-else,getch,printf,scanf etc are used. SMA(STATIC MEMORY ALLOCATION):-                                whenever we create a program,there is a line of declaration statement(or data type declarartion statement) such as int a;char b;etc. Due to this statement we create a variable a,now this variable is the example of SMA.SMA means how much memory is consumed by variable at the time of compiler,this decision is taken before the compilation. At the compile time these variable such as a and b does not having a memory.Memory is given when program is run i.e when object file convert into .exe file.But when program is run how much memory is taken by the variable is decided at the compilation time.This is known as SMA(Static Memory Allocation).   

Compilation Process in C

Compilation The conversion of source code into object code is known as compilation.This process is done with the help of compiler. below is a full diagram of showing how source code convert into software(i.e software development in c) Explanation:- Source code :- the c programming coding is known as source code. save your programe with .c extension then your file become source file for example in fig: sum.c is a source file in which source code is written to print a hello. Prerocessor :- first we need a preprocessor software to convert source file (i.e .c file).Preprocessor handle all those statement which is starting from # symbol. Header file :-  In our programe as seen clearly in sum.c we did not create any header file (i.e which start from # symbol) we just use it.Preprocessor add the content of header file in our source file and creates a new file this file same as our source file but only difference is that the header file removed from the new file which ...