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

Introduction to C

About C:- C is a high-level language . It was initially first developed by D ennis Ritchie in 1972. It was developed to overcome the limitation of previous language such as B,BCPL etc. It was developed for Unix operating System It inherits many features of previous language Features/Advantage:- Simple :- because it provide data types, library function etc. Machine Independent or Portable:- because it can be executed on different machine Procedural language:- Instruction in c program are executed step by step Structured programming language:- break the problem into parts It provide dynamic memory allocation , pointers etc. Speed:-It is faster than another programming language like java, python etc. Disadvantage of C:- It does not support oops concept in which inheritance, polymorphism, encapsulation etc. come. It does not support namespace concept It does not support constructor and destructor There is lack of excepti...

Identifiers in C

Identifiers are:-  constant variables keywords Define constant:- Any information is constant data = information = constant types of constant primary:- integer,real,character secondary:- they are made with the help of primary such as:- array,string,pointer,union,etc. Define variables:- variables are the name of memory location where we store data variable name is any combination of alphabet,digit the first letter cannot be a digit Keywords:-  keywords are the predefined or reserved words.In C there are 31 keywords.C is a case sensitive so use keyword in small alphabets. 

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.