preprocessor
preprocessor
A preprossor is a program that processor or analyzes the source code file before
it is given the compiler.
A preprocessor directive begin with '#' and doesnot ends with semicolon(;)
Directives can placed anywhere in program, but most often placed
at beginning of the program. ie before main().
The Actual purpose of C preprocess is to expand the source code or program.
File Inclusion.
The inclusion directives is #include. This directive instructs the
compiler to include contents
of the specified files. ie. it inserts the entire contents of the file
at that position,
including header file is equals to copying the contents of header files.
By using file Inclusion, we can create our own header file with
different- different function.
Syntax #include<filename>
OR
#include "filename"
Syntax
#define macroname value
| | |
| | |
This statements replaces the macro-name with the given value whenever it appers
from
the first blank and macro-name to the end of the file.
There are three type of macro.
i) Simple Macro
ii) Argumented Macro
iii) Nested Macro.
i) Simple Macro
simple macro is used for to create a constant value in C lang. Once
we define constant value,
we cannot modified value. if we try to modified then its show
compile time error.
Syntax
-----------------------------------------------------------------------------------
-------------------------
#include<stdio.h>
#define PI 3.14
#define A 20
#define B 40
#define Neha "India"
#define Sakshi ;
#define priya }
#define aru printf
int main()
{
printf("\n value of pi =%f",PI);
printf("\n Addition =%d",A+B);
aru("\n Given String is=%s",Neha)Sakshi
return 0;
priya
-----------------------------------------------------------------------------------
-------------
Argumented Macro..
Like normal function, we can pass arguments to macro defination.
It is also called parameterized macro.
But in argumented macro doesnot have datatype checking...
It is preprocess. doesnot show error.
#include<stdio.h>
#define RECT(l,b) printf(" \n Area of rectangle =%d",l*b);
#define SQUARE(x) x*x
#define CUBE(x) x*x*x
int main()
{
int n;
printf("\n Argumented macro....");
RECT(5,6);
printf("\n Enter any number :-->");
scanf("%d",&n);
printf("\n Square %d is--->%d",n,SQUARE(n));
#include<stdio.h>
#define PI 3.14
#define Area(r) PI*r*r
#define square(p) p*p
#define cube(b) b*square(b)
int main()
{
int x;
printf("\n Enter the radius :-> ");
scanf("%d",&x);
printf("\n Kalatey ka Area of circle =%f",Area(x));
printf("\n Square of %d is --->%d",x,square(x));
printf("\n Cube of %d is --->%d",x,cube(x));
return 0;
}
-----------------------------------------------------------------------------------
-------
#include<stdio.h>
#define anu(x) (x)*(x)
int main()
{
printf("\n First Ans =%d",anu(3+5));
return 0;
}
-----------------------------------------------------------------------------------
-------
#include<stdio.h>
#define square(x) (x*x)
int main()
{
int j;
j=64/square(4);
printf("\n Valeu of j =%d",j);
return 0;
}
-----------------------------------------------------------------------------------
--------------------------------
Conditional Compilation.
This directive causes one file to be included in another.
It allows selective inclusion of lines of source text on the basis of
computed condition.
Conditinal compilation is performed using preprocessor directive.
#ifdef #ifndef
#elif
#else #endif
-----------------------------------------------------------------------------------
--------
Error Generation.
This directive displays the error message on occurence of the error.
#error sequnceoftoken
e.g
#error "Error aala re..."
-----------------------------------------------------------------------------------
-----------