C | Macro & Preprocessor | Question 6
#include <stdio.h> #define square(x) x*x int main() { int x; x = 36/square(6); printf("%d", x); return 0; } |
chevron_right
filter_none
(A) 1
(B) 36
(C) 0
(D) Compiler Error
Answer: (B)
Explanation: Preprocessor replaces square(6) by 6*6 and the expression becomes x = 36/6*6 and value of x is calculated as 36. Note that the macro will also fail for expressions “x = square(6-2)”
If we want correct behavior from macro square(x), we should declare the macro as
#define square(x) ((x)*(x))
Recommended Posts:
- C | Macro & Preprocessor | Question 1
- C | Macro & Preprocessor | Question 2
- C | Macro & Preprocessor | Question 14
- C | Macro & Preprocessor | Question 4
- C | Macro & Preprocessor | Question 5
- C | Macro & Preprocessor | Question 7
- C | Macro & Preprocessor | Question 8
- C | Macro & Preprocessor | Question 9
- C | Macro & Preprocessor | Question 15
- C | Macro & Preprocessor | Question 10
- C | Macro & Preprocessor | Question 11
- C | Macro & Preprocessor | Question 12
- C | Macro & Preprocessor | Question 13
- C | Macro & Preprocessor | Question 14
- The OFFSETOF() macro



