The statements starting with the special symbol # are known as preprocessors statements or preprocessor directives.
Preprocessor statements are processed before the program goes to the compiler for processing.
Using preprocessors following can be performed
- Macro definitions
- Macros with arguments
- Conditional compilation
- File inclusion
Macro definitions
- “#define” statement helps us to define a macro.
- Macro once defined becomes a constant.
- Its value cannot be changed during the execution of the program.
- If we try to change the value an error will get generated.
- Macro definition is not terminated by semicolon.
- Each macro name in the program is substituted by the macro value before the program goes to the compiler for compilation.
Example:
#define a 10 #define limit 100 #define num int #define pi 3.1415 #define c clrscr() #define p printf #define g getch();
Question:
Which is better and why?
(a) #define pi 3.1415
(b) float pi=3.1415;
Ans:
(a) is better, as it becomes a symbolic constant its value cannot be changed during the execution of the program and if we try to change the value an error will get generated.
Example:1
#include<stdio.h>
#define limit 100
int main()
{
int i;
for(i=1;i<=limit;i++)
{
printf("%d ",i);
}
return(0);
}
Example:2
#include<stdio.h>
#include<conio.h>
#define p printf
#define s scanf
//#define c clrscr();
#define g getch()
#define num int
#define hello int
#define ankur main()
hello ankur
{
num a,b;
//c
p("enter any number ");
s("%d",&a);
b=a*a;
p("square is %d\n",b);
g;
return(0);
}
/* Output */ enter any number 2 square is 4
Example:3
#include<stdio.h>
#include<conio.h>
#define begin {
#define end }
#define bs (
#define be )
#define hello void
#define mukesh main
#define p printf("hello");
//#define c clrscr();
#define g getch();
hello mukesh bs be
begin
//c
p
g
end
/* Output */ hello
Example:4
#include<stdio.h>
#include<conio.h>
#define c {
#define g }
#define a void
#define b main()
#define e printf("Hello How are you");
//#define d clrscr();
#define f getch();
a b c //d
e f g
/* Output */ Hello How are you
Example:5
#include<stdio.h>
#include<conio.h>
#define a void
//#define b main(){clrscr();printf("hello");getch();}
#define b main(){printf("hello");getch();}
a b
/* Output */ hello
Example:6
#include<stdio.h>
#include<conio.h>
//#define a main(){clrscr();printf("hello");getch();return(0);}
#define a main(){printf("hello");getch();return(0);}
a
/* Output */ hello




