Can the following macro bring problems?
#define sq(x) x*x
If yes, then how and why?please help.
Yes, it can present problems. Other than the obvious fact that macros don't respect namespaces at all (which means you can't call anything else sq
), try the following:
int result = sq(4) / sq(4);
You should surround x * x
with parenthesis so it becomes ((x) * (x))
.
Another problem:
int a = 0;
int result = sq(++a);
This is an inherent problem with macros, and is one reason inline functions should be preferred.