Professional Documents
Culture Documents
Pre Processor
Pre Processor
Preprocessor Directives
The preprocessor, is a program that processes the source program before
compilation.
Preprocessor directives are placed in the source program before the main line.
If there are any, appropriate actions (as per the directives) are taken and then
the source program is handed over to the compiler.
General Form
Preprocessor directives follow special syntax rules that are different
from the normal C syntax.
They all begin with the symbol # in column one and do not require
a semicolon at the end.
#define identifier substitute
or
#define identifier(argument-1 . . . argument-N) substitute
Commonly Used Preprocessor Directives
Categories of Directives
It operates under the following directives
File Inclusion
Macro substitution
Conditional inclusion
File Inclusion
It is used to include some file that contains functions or
some definitions.
Syntax:
#include<filename> (or)
#include“filename”
Eg: #include<stdio.h>
#include “ex.c”
Example
#include<stdio.h>
#include "addition.txt"
Int main()
{
int a,b;
printf("\nEnter the numbers:");
scanf("%d%d",&a,&b);
printf("The Value is %d",add(a,b));
return 0;
}
addition.txt
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
Output
Output:
The Value is 6.280000
The Value CITY is chennai
Argumented Macro
It is used to define some complex forms in the source program.
Syntax:
#define identifier (v1,v2,….) string/integer
Eg:
#define cube(n) (n*n*n)
Example
#include<stdio.h>
#define cube(n) (n*n*n)
int main()
{
printf("The Value of 3 cube is %d",cube(3));
return 0;
}
Output:
The Value of 3 cube is 27
Nested Macro
Eg:
#define a 3
#define sq a*a
Example
#include<stdio.h>
#define a 3
#define sq a*a
int main()
{
printf("The Value is %d",sq);
return 0;
}
Output:
The Value is 9
Conditional Inclusion
Output:
The value C is 8
What will be the output of the following C code?
#include <stdio.h>
#if A == 1
#define B 0
#else
#define B 1
#endif
int main()
{
printf("%d", B);
return 0;
}
Output:
1