Avoid macros in C
Macros in C are problematic. This explained via example as follows
#define CHECK_INT(x) (x != 10 && x >= 20 && x <= 100)
while (CHECK_INT(val = get_value_from_void() )) {
....
}
Now the problem occurs when x inside the problem fails a check. Each time a check fails, get_value_from_void is called again to get a new value of x.
Hence be careful while using macros in C. Follow the rules given below to avoid problems
1) Make sure the argument to the macros are evaluated before passing them to the macro
2) Make sure to parenthise the macro body and it's arguments.
3) DO NOT use macro definitions to define constants in C, use enum instead
Labels: C
0 Comments:
Post a Comment
<< Home