#include<stdio.h>
#include<conio.h>
#define square(v) v*v
void main()
{
int p=3;
int s=square(++p);
printf("%d %d",s,p);
getch();
}
輸出爲什麼16 4不是輸出? (Advance thanks)#定義奇怪
#include<stdio.h>
#include<conio.h>
#define square(v) v*v
void main()
{
int p=3;
int s=square(++p);
printf("%d %d",s,p);
getch();
}
輸出爲什麼16 4不是輸出? (Advance thanks)#定義奇怪
的
++p * ++p
的行爲是不確定的,它取決於編譯器
您可以使用inline將
inline int square(int p) {
return p * p;
}
見示例http://stackoverflow.com/questions/3605005/evaluate-macro-parameter-once-only解決方法。 – Cascabel