为什么程序的输出结果是2?#if后面的表达式的值不是1吗?求各位大神解答!
int main()
{
int a=1;
#if a
cout<<1;
#else
cout<<2;
#endif
}
int main()
{
#define a 1
#if a==1
cout<<1;
#else
cout<<2;
#endif
}
{
#define a 1
#if a==1
cout<<1;
#else
cout<<2;
#endif
}
这个是你要的结果。
宏定义,是编译时完成,变量定义,是运行时完成。编译在前。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-10-30
int main()
{
int a=1;
#if a==0
cout<<1;
#else
cout<<2;
#endif
}
这段代码输出1
我想你明白了吧
a的赋值位置不是在编译时追问
{
int a=1;
#if a==0
cout<<1;
#else
cout<<2;
#endif
}
这段代码输出1
我想你明白了吧
a的赋值位置不是在编译时追问
现在a是1,(a!=1)的值应该是0啊,为什么就执行第一个程序段了呢?
追答现在a的值是0
运行之后a才是1