C Preprocessor(C Program ) Questions and Answers

Question 1. C preprocessor
  1.    Takes care of conditional compilation
  2.    Takes care of macros
  3.    Takes care of include files
  4.    Acts before compilation
  5.    All of the above
Explanation:-
Answer: Option E. -> All of the above

Question 2. What will be the output of the program?
#include
#define int char
void main()
{
int i = 65;
printf("sizeof(i)=%d", sizeof(i));
}
  1.    sizeof(i)=2
  2.    sizeof(i)=1
  3.    Compiler Error
  4.    None of These
Explanation:-
Answer: Option B. -> sizeof(i)=1
Since the #define replaces the string int by the macro char.
So, here i is a variable of type char and not int.

Question 3. What will be the output of the following program?
#include
#define square(x) x*x
void main()
{
int i;
i = 64/square(4);
printf("%d", i);
}
  1.    4
  2.    64
  3.    16
  4.    None of These
Explanation:-
Answer: Option B. -> 64
The macro call square(4) will be substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority and associativity left to right, so the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64.

Question 4. What will be the output of the program code?
#include
#define a 10
void main()
{
#define a 50
printf("%d", a);
}
  1.    50
  2.    10
  3.    Compiler Error
  4.    None of These
Explanation:-
Answer: Option A. -> 50
The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.

Question 5. A preprocessor command
  1.    need not start on a new line
  2.    need not start on the first column
  3.    has # as the first character
  4.    comes before the first executable statement
Explanation:-
Answer: Option C. -> has # as the first character

Question 6. Choose the correct statement.
I.   The scope of a macro definition need not be the entire program.
II.  The scope of a macro definition extends from the point of definition to the end of the file.
III. New line is a macro definition delimiter.
IV.  A macro definition may go beyond a line.
  1.    I and II
  2.    II and III
  3.    I, II and III
  4.    II, III and IV
  5.    I, II, III and IV
Explanation:-
Answer: Option E. -> I, II, III and IV

Question 7. What will be output if you will compile and execute the following c code?
#include
#define max 5
void main(){
int i = 0;
i = max++;
printf("%d", i++);
}
  1.    5
  2.    6
  3.    7
  4.    0
  5.    Compiler Error
Explanation:-
Answer: Option E. -> Compiler Error
main.c: In function ‘main’:
main.c:5:12: error: lvalue required as increment operand
i = max++;

Question 8. In which stage the following code
#include
gets replaced by the contents of the file stdio.h
  1.    During Preprocessing
  2.    During Execution
  3.    During linking
  4.    During Editing
  5.    None of these
Explanation:-
Answer: Option A. -> During Preprocessing

Question 9. What will be the output of the following program?
#include
#define prod(a,b) a*b
void main()
{
int x=3,y=4;
printf("%d", prod(x+2,y-1));
}
  1.    15
  2.    12
  3.    10
  4.    11
  5.    None of these
Explanation:-
Answer: Option C. -> 10
The macro expands and evaluates to as:
x+2*y-1 => x+(2*y)-1 => 10

Question 10. Determine output:
#include
#define clrscr() 100
void main()
{
clrscr();
printf("%dn", clrscr());
}
  1.    0
  2.    1
  3.    100
  4.    Error
Explanation:-
Answer: Option C. -> 100
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\n", 100);
}