Saturday, February 16, 2008


Preprocessor directives

(1)What is preprocessor ?
Ans:

All the preprocessor are not part of c program. It is only instruction to compiler.
All the preprocessor process before the staring of actual compilation and create an intermediate file.
In the intermediate file all preprocessor is converted in term of actual c.
To see the intermediate file :
Step 1: First create any c file let us assume test. c which contain :
#define max 10+2
void main()
{
int a;
a=max*max;
printf("%d",a);
}
Step 2: go to command mode. Open run then write cmd then press enter.
Step 3: Go to the directory where test .c has been created.
Step 4: write in the command mode
cpp test. c and press enter key (to create intermediate file)
Step 5: type test. i (to see the intermediate file )

(2) Why preprocessor ?
Ans :
1. It improves the readability of program.
2. It makes easy to modify
3. portability

(2) List all preprocessor directives.
Ans:

#define

(4) Explain macro substitution directive?
Ans:
Syntax:
#define [ ,, …]
Here [,,…] is optional.
When you use this in the program then in the program this is called macro and

#define directive only replaces the macro by before starting of actual compilation.
e.g
#define pie 3.14
Void main()
{
float r=3,area;
area=3*r*pie;
printf("%f",area);
getch();
}
Before the starting of actual compilation an intermediate is formed which is :

We can see only in place of pie ,3.14 has pasted.
If is very long or we want to write in next line ,end first line by \.
e.g
#define word c is powerful \
language.
MACRO FUNCTION:

#include

(6) What is file inclusion directive or #include directive ?
Ans:
Syntax:


#include

or

#include "file name"
This directive treats has included in the current file i.e in current file also contain

data of the file which has included by #include directive.
e.g
first create a file of file name cube.h which contain :
int cube ( int a)
{
Int b;
B=(a)*(a)*(a);
Return b;
}
Now create any another c file let math.c which contain :
#include "cube.h"
void main()
{
int p=5,q;
q=cube(p);
printf("%d",q);
}
Output :125
Explanation:

When we write #include "cube.h" the compile includes the contain of file cube.h

i.e cube function in the math.c file.So we can use the function which has defined in cube.h .

We can see the intermediate file of math.c :

#include : Search the file only include directory (c:\tc\include )
#include "filename" : Search the file include directory as well as current working directory.
Note : In the above question if you will save the file cube.h at c:\tc\include directory then you can write in math.c :


0 comment here::