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

Type casting

type casting or type conversion  allows us to convert one data types into another.for type casting cast operator is required. Syntax:--      (type)expression or value; Example:       int f =9/4; //without type casting       o/p=2       float f=(float)9/4;  //with type casting       o/p=2.3 Type conversion are of two types: 1)Implicit Type Conversion:-  it is also known as automatic type conversion.When the type conversion is performed automatically by the compiler,such type of conversion is known as implicit type conversion. 2)Explicit Type Conversion:-  the type conversion performed by the programmer is known as explicit type conversion.which is done by cast operator as above mentioned.

UNION

Definition:-                 Union is similar to structure, except it allows you to define the variable that share storage spaces.Defining a means creating a new data types. How use union:-                       when we create a structure by using struct keyword, now making a union we just replace the keyword struct with the keyword union , other syntax will be same. Difference  between Structure  and Union:- the only difference between them is in memory. NOTE:-  union is used for low level programming but structure mostly used for high level programming. Program to access union:- """SHARE IT WITH YOUR FRIENDS IF HE DID NOT KNOW""""

Recursion--

Definition:- when a function call itself is called recursion. note:- the problem which is solved by recursion is also solved by loops, but it is not necessary the problem solved by loops is also solved by recursion.  Example:- Explanation:- 1) a fun function declared which takes argument and return value 2) main starts fun is called by passing 3 value 3)after calling fun definition starts, where check if a==1 no because a is 3, s=a+fun(a-1) means s=3+fun(2)  from here fun function is again called now this body again execute now a is 2, so s=3+fun(2), fun(2) is replaced by 2+fun(2-1=1) i.e s=3+2+fun(1) again fun function is called... overall s=3+2+1=6, s is return this return value goes where fun function is called , where return value is stored in k, so k=6 now print is 6.