C Multiple Choice Questions & Answers on “Conditional Preprocessor Directives – 2”.
1. What will be the output of the following C code?
#include#define san 10 main() { #ifdef san #define san 20 #endif printf("%d",san); }
a) 10
b) 20
c) Error
d) 1020
Answer: b
Clarification: In the code shown above, if the identifier san is defined, then its value is redefined and changed to 20. It is then printed. Hence the output is 20.
2. What will be the output of the following C code?
#include#define hello main() { #ifdef hello #define hi 4 #else #define hi 5 #endif printf("%d",hi); }
a) 4
b) 5
c) 45
d) error
Answer: a
Clarification: The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is 4. If hello was not defined, then the value of hi would be 5.
3. The purpose of the preprocessor directive #error is that ____________
a) It rectifies any error present in the code
b) It rectifies only the first error which occurs in the code
c) It causes the preprocessor to report a fatal error
d) It causes the preprocessor to ignore an error
Answer: c
Clarification: #error is a preprocessor directive which causes the preprocessor to report a fatal error. The tokens which form the rest of the line after #error are used as the error message.
4. What will be the output of the following C code?
#include#define max 20 main() { #ifndef max #define min 10 #else #define min 30 #endif printf("%d",min); }
a) 10
b) 20
c) 30
d) error
Answer: c
Clarification: The output of the code shown above is 30. Since the identifier max is defined (the condition of #ifndef is true), hence another identifier, that is, min is defined and its value is equal to 30.
5. What will be the output of the following C code?
#include#define hello 10 main() { #ifdef hello #undef hello #define hello 100 #else #define hello 200 #endif printf("%d",hello); }
a) Error
b) 10
c) 100
d) 200
Answer: c
Clarification: The output of the code shown above is 100. If the identifier hello is defined, then it is undefined and redefined. It’s new value is 100. If the identifier was not defined, it would be defined with a value of 200.
6. What will be the output of the following C code?
#include#define sf 10 main() { if(sf==100) printf("good"); else { printf("bad"); sf=100; } printf("%d",sf); }
a) 100
b) bad
c) 10
d) error
Answer: d
Clarification: The above code will result in an error because a macro cannot be defined using a simple assignment operator. In the code shown above, the value of sf is changed to 100 using the assignment operator. This causes an error.
7. What will be the output of the following C code?
#includevoid f() { #define sf 100 printf("%d",sf); } int main() { #define sf 99; f(); printf("%d",sf); }
a) error
b) 100
c) 99
d) 10099
Answer: a
Clarification: Macro definition is global even if it is appears within the function scope. In this case the macro sf is defined in the function f(). Hence it results in a compile time error.
8. What will be the output of the following C code?
#include#define INDIA 1 #define US 2 #define CC US main() { #if CC==INDIA printf("Rupee"); #elif CC==US printf("Dollar"); #else printf("Euro"); #endif }
a) Euro
b) Rupee
c) Dollar
d) Error
Answer: c
Clarification: The output of the code shown above is Dollar. Since the macro CC is defined as US at the beginning of the code, it satisfies the condition #elif(CC==US). Hence “Dollar” is printed.
9. What will be the output of the following C code?
#define sqr(x) x*x main() { int a1; a1=25/sqr(5); printf("%d",a1); }
a) 25
b) 1
c) 5
d) error
Answer: a
Clarification: The output of the code shown above is 25. This is because the arithmetic statement takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x) (x*x), the output would have been 1.
10. Which of the following is not a preprocessor directive?
a) #error
b) #pragma
c) #if
d) #ifelse
Answer: d
Clarification: #ifelse is not a preprocessor directive. #error, #pragma, #if are preprocessor directives. There is a preprocessor directive, #elif, which performs the function of else-if.