c - Macro argument with different results -
this question has answer here:
- the need parentheses in macros in c 8 answers
so have been having rather 'unyielding' problem macro call in c program. macro used :
#define macro(x) x*x
and problem when
printf("%d",macro(3));
it displays 9 result (which correct). when pass 3 2+1 follows :
printf("%d",macro(2+1));
it strangely displays outcome of 5. can please have me informed why?
printf("%d",macro(2+1));
after preprocessing become
printf("%d",2+1*2+1);
since multiplication has high precidenece on addition, print 5;
to resolve issue have define macro follows
#define macro(x) (x)*(x)
Comments
Post a Comment