Preprocessing and Macros
- Which of the following statements are true.
- Preprocessing generates object files.
Ans: False. Preprocessing generates code for compilation.
- #include directive includes a whole file as is at the location of the directive.
Ans: True.
- In case included file was not found, system will skip its preprocessing and advance to the next stage.
Ans: False. Compilation will fail.
- Linker links function calls across object files.
Ans: True.
- Which of the following statements are true.
- #define macro will replace only first occurrence of macro name with macro body.
Ans: False. All occurences after macro definition will be replaced.
- #define macro can be used only for replacing literals.
Ans: False. #define can be used to replace anything, including literals.
- #define macro cannot be placed in a .h file.
Ans: False. They can be placed anywhere.
- 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.
- Which of the following statements are true.
- In a macro, arguments are passed by value.
Ans: False. There is no passing of arguments, it is textual substitution.
- Macro arguments data types are matched and validated during preprocessing phase.
Ans: False. Textual substitution does not involve any data typ matching.
- Macro arguments are textually substituted, as is, just like macro body.
Ans: True.
- 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.
- Write a macro that takes 3 arguments and computes their sum.
#define SUM(A,B,C) A+B+C
- 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);\ }
- 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.
- Which of the following statements are true.
- #if directive is used to decide what code gets included in the binary executable.
Ans: True.
- 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.
- Which of the following statements are true.
- 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.
- Multiple inclusion of header files can lead to compilation error.
Ans: True.
- Use of #ifndef to avoid multiple inclusion of header files requires use of a unique symbol for every header file.
Ans: True