Monday, December 1, 2008

PREPROCESSOR Question and Answer for Placement in C programming language

1) #define max 10
void main()
{
int i;
i=++max;
clrscr();

printf("%d",i);
getch();
}

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();
}
In this program we are trying to incrment a constant symbol.
meanig of ++10 IS :-
10=10+1
or 10=11
which is not true.Hence compiler will give error.


2)
#define max 10+2
void main()
{
int i;
i=max*max;
clrscr();
printf("%d",i);
getch();
}
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)
#define A 4-2
#define B 3-1
void main()
{
int ratio=A/B;
printf("%d ",ratio);
getch();
}
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)
#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();
}

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)
#define START main() {
#define PRINT printf("*******");
#define END }
START
PRINT
END

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

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();
}

No comments:

Post a Comment

Thanks to given comments.......

My Blog List