// TEST1.cpp : Defines the entry point for the console application. #include "stdafx.h" int main(int argc, char* argv[]) int i;
//printf("STR与name相等!\n",STR,name); //printf("STR与name相等!\n",STR,name);
#define for if(1)for
|
以上代码运行结果:
如果把上述代码改一下:
x264 common/common.c
#define OPT(STR) else if( !strcmp( name, STR ) )
if( (!strncmp( name, "no-", 3 ) && (i = 3)) || (!strncmp( name, "no", 2 ) && (i = 2)) ) /* ?i在这儿怎么会大于0呢? */ #define OPT(STR) else if( !strcmp( name, STR ) ) /* strcmp(const char *s1,const char * s2); 比较字符串s1和s2。 说明: 当s1<s2时,返回值<0 ; 当s1=s2时,返回值=0 ; 当s1>s2时,返回值>0 , 即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。 */ |
其它:
根据C++标准:
for(int i = 0; i < 5; i ++
{
...
}
i的作用域应该是在花括号内。而VC6不符合标准,i的作用域会一直持续到后面:
int main()
{
for(int i = 0; i < 5; i ++
{
cout < < i;//正确
}
cout < < i;//按照标准应该错误,但在VC6中正确
int i;//按照标准应该正确,但在VC6中是重定义错。
}
因此在VC6中通常定义这样一个宏,这个上面的语句在宏被展开时就成了:
if (0)
else
for(int i = 0; i < 5; i ++)
...
这样i的定义就不会“遗害后人”了。