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();
}
Sunday, 25 October 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment