Output of the Program | Use Macros Carefully!
Predict the output of the below program
#define square(x) x*x int main() { int x; x = 36/square(6); printf ( "%d" ,x); getchar (); return 0; } |
chevron_right
filter_none
Output: 36
Explanation:
Preprocessor replaces square(6) by 6*6 and the expression becomes x = 36/6*6 and value of x is calculated as 36. If we want correct behavior from macro square(x), we should declare it as
#define square(x) ((x)*(x)) /* Note that the expression
(x*x) will also fail for square(6-2) */
Recommended Posts:
- Output of C++ programs | Set 25 (Macros)
- Macros vs Functions
- Importance of macros in C++
- Interesting Facts about Macros and Preprocessors in C
- Output of C Program | Set 22
- Output of C++ Program | Set 14
- Output of C++ Program | Set 15
- Output of C++ Program | Set 16
- Output of C++ Program | Set 17
- Output of C++ Program | Set 18
- Output of C++ Program | Set 13
- Output of C Program | Set 27
- Output of C++ Program | Set 12
- Output of C++ Program | Set 10
- Output of C Program | Set 23