Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

preprocessor

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

preprocessor

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 10 Preprocessor

Before the complication some process are done is called preprocessor.


A preprocessor plays an important role in the program execution process.
In the C Program developement cycle, the source code( .c) is given to the
preprocessor
before it is compiled.

A preprossor is a program that processor or analyzes the source code file before
it is given the compiler.

Preprocessor compile the following tasks.


i) It takes action according to special instructions called Preprocessor
directive which begin with #.
ii) Joins any lines that end with a backslash character into a single line.
iii) Divides the program into a stream of tokens.
iv) Remove comments and replaces them by a single space.
v) processes preprocessor directives and expands macros.
vi) Replaces escape sequence by their equivalent internal represenation.
vii) Concatenates adjacents constants character Strings.
viii) Replaces trigraph sequence (Non Ascii characters sets) by their
equivalent.

Format of Preprocessor directives.


The preprocessor checks the source code for special instructions called
preprocessor directives.
Preprocessor directives are not C lang statements but they are special
instructions
for the preprocessor.
C preprocessor is a collection of special statements called Directives.
Either it can be independent program or its functionality may be embedded
in 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.

Preprocessor directives are divided into Four types.


i) File inclusion.
ii) Macro Substitution.
iii) Conditional Compilation.
iv) error Generation.

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"

In the first format, the filename is searched in standard


directories only. and
Second format (#include "filename") the file is first searched in the
current directiory.
If it is not found then the search continues in the standard include
directory.

Any external file containing user defined functions, macro defination


etc, can be included.
An included file can include other files.
---------------------------------- fileinclusion.c -------------------------
#include<stdio.h>
#include<conio.h>
#include"rasika.h"
#include"varsha.h"
#include"aditya.h"
void main()
{
clrscr();
printf("\n Demo of file inclusion ");
add(10,20); //calling
fact(5);
pattern();
rectarea(4,5);
sub(60,32);
getch();
}
--------------------------------------------------aditya.h
---------------------------
void sub(int a,int b)
{
printf("\n Subtraction =%d",a-b);
}
------------------------------------------------------------rasika.h
-----------------------------
void add(int x,int y)
{
printf("\n Addition =%d ",x+y);
}
void fact(int n)
{
int f=1;
printf("\n Factorial of %d is --> ",n);
while(n>1)
{
f=f*n;
n--;
}
printf("%d",f);
}
--------------------------------------------varsha.h
-----------------------------------------
void rectarea(int l,int b)
{
printf("\n Area of rectangle =%d",l*b);
}
void pattern()
{
int i,j,n;
printf("\n How many line u want :->");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=i;j++)
{
printf(" %d ",j);
}
printf("\n");
}
}
-------------------------------------------------------------
Macro..
Macro is single line abbrevation function.
Macro is preprocessor, it executed before the compilation. hence it doesnot have
error checking,
and datatype check. Macro defination we can define by using '#define'
preprocessor.

This directives replaces every occurence of a simple macro in the program.


It is obtained using #define directive.

Syntax
#define macroname value
| | |
| | |

macro macro macro


defination template expansion.

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

#define macroname value

//WAP to demonstrate of simple macro


#include<stdio.h>
#define A 20
#define B 23
#define shreyash printf
#define anu ;
#define adi }
void main()
{
printf("\n value of A=%d and B=%d",A,B)anu
shreyash("\n Addition =%d",A+B);
//A=A+70; macro cannot modified...
adi

-----------------------------------------------------------------------------------
-------------------------
#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.

Syntax #define macroname(arguments) expression

#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));

printf("\n Cube of %d is--->%d",n,CUBE(n));


return 0;
}
--------------------------------------------------------------------------
#include<stdio.h>
#define PI 3.14
#define Max(a,b) a>b ? printf("\n %d is max",a) : printf("\n %d is max",b);
#define area(r) printf("\n Area of circle =%f",PI*r*r);
#define rect(l,b) printf("\n area of rectangle =%d",l*b);
#define avg(a,b,c) printf("\n Average of theree num=%f",(float)(a+b+c)/3);
int main()
{
int x,y;
printf("\n Enter the value of x and y:--->");
scanf("%d %d",&x,&y);
Max(x,y);
area(5);
rect(x,y);
avg(64,74,23);
printf(" \n Kalatey ka ? ");
return 0;
}
-------------------------------------------------------------------------
Nested Macro...
One macro is define in another macro is called Nested macro.
That is macro defination can be nested.

#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));

printf("\n Second Ans =%d",anu(5-2));

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

#ifndef is opposite of #ifdef directive. #ifdef includes the code if the


identifier is defined before but
#ifndef includes the code if identifier has not been define before.

#elif is similar to else if construct.

-----------------------------------------------------------------------------------
--------
Error Generation.
This directive displays the error message on occurence of the error.

#error sequnceoftoken

e.g
#error "Error aala re..."

-----------------------------------------------------------------------------------
-----------

You might also like