(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.h”
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 :
#include


#,##

(5) What is use of # and ## operator in c program ?
Ans:


There are two operator in preprocessor.
1. # this operator is called stringizing operator which convert any argument in the macro function in the string. So we can say pound sign # is string maker.
e.g
#define string(s) #s
void main()
{
char str[15]=string(World is our ) ;
printf(“%s”,str);
}
Output: World is our
Explanation : Its intermediate file is :


Argument of string macro function ‘World is our’ is converted into string by the operator # .Now the string constant “World is our” is replaced the macro call function in line number 4.
2. ##


This operator is called token pasting operator. When we use a macro function with various argument then we can merge the argument with the help of ## operator.
e.g
#define merge(p,q,r) p##q##r
Void main()
{
int merge(a,b,c)=45;
printf(“%d”,abc);
}
Output : 45
Explanation :
Arguments a,b,c in merge macro call function is merged in abc by ## operator .So in the intermediate file declaration statement is converted as :
int abc=45;

#pragma

(6) Describe #pragma directive in detail ?
Ans:
Pragma is implementation specific directive i.e each pragma directive has different implementation rule and use . There are many type of pragma directive and varies from one compiler to another compiler .If compiler does not recognize particular pragma the it ignore the pragma statement without showing any error or warning message and execute the whole program assuming this pragma statement is not present,
e.g
suppose any arbitrary pragma directive is #pragma world :


#pragma world
void main()
{
printf(“C is powerful language “);
}
Output : C is powerful language
Explanation:

Since #pragma world is unknown for Turbo c compiler so it ignore this directive without showing error or warning message and execute the whole program assuming #pragma world statement is not present.
List of pragma directive :
1. #pragma startup
2. #pragma exit
3. #pragma warn
4. #pragma option
5. #pragma inline
6. #pragma argsused
7. #pragma hdrfile
8. #pragma hdrstop
9. #pragma saveregs

Question section

(1)

What will be output of following code?

#define max 10

void main()

{

int i;

i=++max;

clrscr();

printf("%d",i);

getch();

}

(2)

What will be output of following code?

#define max 10+2

void main()

{

int i;

i=max*max;

clrscr();

printf("%d",i);

getch();

}

(3)

What will be output of following code?

#define A 4-2

#define B 3-1

void main()

{

int ratio=A/B;

printf("%d ",ratio);

getch();

}

(4)

What will be output of following code?

#define MAN(x,y) (x)>(y)?(x):(y)

void main()

{

int i=10,j=9,k=0;

k=MAN(i++,++j);

printf("%d %d %d",i,j,k);

getch();

}

(5)

What will be output of following code?

#define START main() {

#define PRINT printf("*******");

#define END }

START

PRINT

END

(6)

What will be output of following code?

#define CUBE(x) (x*x*x)

#define M 5

#define N M+1

#define PRINT printf("RITESH");

void main()

{

int volume =CUBE(3+2);

clrscr();

printf("%d %d ",volume,N);

PRINT

getch();

}

Best questions of preprocessor questions

Big collection of preprocessor questions

Solution section
To read theory click here

(1)
output: compiler error.
explanation:
max is preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value in entire the program before the compilation.so in this progrom max will be replaced by 10 before compilation.Thus program will be converted as:

void main()

{

int i;

i=++10;

clrscr();

printf("%d",i);

getch();

}


To know how we can see intermediate file click here
In this program we are trying to incrment a constant symbol.
meanig of ++10 IS :-
10=10+1
or 10=11
which is error because we cannot assigne constant value to another constant value .Hence compiler will give error.

(2)
output: 32

explanation:
max is preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calulation in entire the program before the
compilation.so in this progrom max will be replaced by 10+2 before compilation.Thus program
will be converted as:


void main()

{

int i;

i=10+2*10+2;

clrscr();

printf("%d",i);

getch();

}


now i=10+2*10+2
i=10+20+2
i=32

(3)
output : 3
explanation:

A and B preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the
compilation.so in this progrom A and B will be replaced by 4-2 and 3-1 respectively before compilation.Thus program
will be converted as:


void main()

{

int ratio=4-2/3-1;

printf("%d ",ratio);

getch();

}



here ratio=4-2/3-1
ratio=4-0-1
ratio=3

(4)
output: 11 11 11

explanation:
Preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the
compilation.Thus program will be converted as:


void main()

{

int i=10,j=9,k=0;

k=(i++)>(++j)?(i++):(++j);

printf("%d %d %d",i,j,k);

getch();

}


now k=(i++)>(++j)?(i++):(++j);
first it will check the condion
(i++)>(++j)
i++ i.e when postfix is used with variable in expression then expression is evaluated first with original
value then variable is incemented
or 10>10
this condition is false.
now i=10+1=11
there is rule , only false part will execute after ? i.e ++j ,i++ will be not execute.
so after ++j
j=10+1=11;
and k will assign value of j .so k=11;

(5)
output: *******
explanation:
Preprocesor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value i.e symbol without any calculation in entire the program before the compilation.Thus program will be converted as:


main()

{

printf("*******");

}

(6)
output: 17 6
explanation:
Preprocessor macro which process first before the actual compilation.
preprocessor paste the symbol to the its constant value without any calculation in entire the program before the compilation.Thus program will be converted as:

void main()

{

int volume =(3+2*3+2*3+2);

clrscr();

printf("%d %d ",volume,5+1);

PRINT

getch();

}

0 comments:

Followers

 
C - Programming -