开发者

#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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜