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.
#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
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
Post a Comment