#define strange
#include<stdio.h>
#include<conio.h&g开发者_JAVA百科t;
#define square(v) v*v
void main()
{
int p=3;
int s=square(++p);
printf("%d %d",s,p);
getch();
}
output 25 5 Why 16 4 is not coming as output? (Advance thanks)
A macro is basically a text copy and paste. Therefore your ++
is being duplicated.
The macro is being expanded as:
s = ++p * ++p;
That's the danger of macros. (in this case, it also invokes undefined behavior)
the behavior of
++p * ++p
is undefined, it depends on the compiler
You may use inline instead
inline int square(int p) {
return p * p;
}
精彩评论