Preprocessing and Macros

  1. Which of the following statements are true.
    1. Preprocessing generates object files.

Ans: False. Preprocessing generates code for compilation.

  1. #include directive includes a whole file as is at the location of the directive.

Ans: True.

  1. In case included file was not found, system will skip its preprocessing and advance to the next stage.

Ans: False. Compilation will fail.

  1. Linker links function calls across object files.

Ans: True.

 

  1. Which of the following statements are true.
    1. #define macro will replace only first occurrence of macro name with macro body.

Ans: False. All occurences after macro definition will be replaced.

 

  1. #define macro can be used only for replacing literals.

Ans: False. #define can be used to replace anything, including literals.

 

  1. #define macro cannot be placed in a .h file.

Ans: False. They can be placed anywhere.

 

  1. Just like a variable, we cannot define a macro more than once in same scope.

Ans: False. Macros can be redefined. Most recent definition take effect.

 

 

  1. Which of the following statements are true.
    1. In a macro, arguments are passed by value.

Ans: False. There is no passing of arguments, it is textual substitution.

 

  1. Macro arguments data types are matched and validated during preprocessing phase.

Ans: False. Textual substitution does not involve any data typ matching.

 

  1. Macro arguments are textually substituted, as is, just like macro body.

Ans: True.

 

 

  1. Point out the error in below program.
#include <stdio.h>
#define MAX(A,B)  (A>B)?A:B
int main(int argc, char *argv[]) {
    int num1 = 5, num2 = 7;
    int max_of_two = 0;
    max_of_two = MAX(num1, B);
    printf ("Max of two numbers is %d\n", max_of_two);
}

 

Ans: Second argument of macro MAX is B (line 06) which is undefined variable in main. It should have been num2 instead.

 

  1. Write a macro that takes 3 arguments and computes their sum.
#define SUM(A,B,C) A+B+C

 

  1. Write a macro that takes three int arguments and prints the highest value.

#define MAX(A,B,C) if(A>=B && A>=C)  {\
    printf("%d\n",A);\
} else if(B>C) { \
    printf("%d\n",B);\
} else {\
   printf("%d\n",C);\
}

 

  1. Condition of #if directive cannot contain a variable. Why?

Ans: #if is used to include/exclude segments of code from compilation process. Variables and their values come into existence much later when a compiled binary is executed. Hence #if cannot use variables.

 

  1. Which of the following statements are true.
    1. #if directive is used to decide what code gets included in the binary executable.

Ans: True.

  1. Code in #elif gets compiled in irrespective of value of #if

Ans: False. #elif behaves like else if. If value of #if was true, whole #else or #elif block will be excluded.

 

  1. Which of the following statements are true.
    1. We cannot define a symbol again until we have undefined it.

Ans: False. #undef is not required for using #define again for the same symbol.

 

  1. Multiple inclusion of header files can lead to compilation error.

Ans: True.

 

  1. Use of #ifndef to avoid multiple inclusion of header files requires use of a unique symbol for every header file.

Ans: True