C Preprocessor - Study Mode
[#56] What will be the output of the program code? #include<stdio.h>
#define a 10
void main()
{
#define a 50
printf("%d", a)
}
Correct Answer
(A) 50
Explanation
Solution: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
[#57] Determine output: #include<stdio.h>
#define clrscr() 100
void main()
{
clrscr()
printf("%dn", clrscr())
}
Correct Answer
(C) 100
Explanation
Solution: Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this : void main ()
{ 100
printf ( " %d
", 100 )
} Note: 100
is an executable statement but with no action. So it doesn't give any problem.
[#58] What will be the output of the following program? #include<stdio.h>
#define prod(a,b) a*b
void main()
{
int x=3,y=4
printf("%d", prod(x+2,y-1))
}
Correct Answer
(C) 10
Explanation
Solution: The macro expands and evaluates to as: x+2*y-1 => x+(2*y)-1 => 10
[#59] What will be output if you will compile and execute the following c code? #include<stdio.h>
#define max 5
void main(){
int i = 0
i = max++
printf("%d", i++)
}
Correct Answer
5
Explanation
Solution: main.c: In function ‘main’: main.c:5:12: error: lvalue required as increment operand i = max++
[#60] What will be output after executing following code? #include<stdio.h>
# define a 10
void main()
{
printf("%d..", a)
foo()
printf("%d", a)
}
void foo()
{
#undef a
#define a 50
}
Correct Answer
(B) 10..10