-1

我编写了以下 C 代码:

#define MAX_T(a,b,d) \
    { int len = (d);printf("len %d", len);}

int main()
{
    //MAX_T(10,30, smgarbage)
    MAX_T(10,30, len)
}

当我注释掉MAX_T(10,30, smgarbage)并运行它时,它会输出len 24. 当我注释掉MAX_T(10,30, len)并运行时,MAX_T(10,30, smgarbage)我得到这个编译器错误:

test.c: In function 'main':  
test.c:34: error: 'smgarbage' undeclared (first use in this function)  
test.c:34: error: (Each undeclared identifier is reported only once  
test.c:34: error: for each function it appears in.)

执行时MAX_T(10,30, len)为什么没有出现编译器错误?是声明int len = (d);造成的吗?如何?

4

1 回答 1

6

宏替换后,MAX_T(10,30, smgarbage)将是:

{ int len = (smgarbage);printf("len %d", len);}

显然没有声明smgarbage

MAX_T(10,30, len)将会:

{ int len = (len);printf("len %d", len);}

尽管输出未知,因为len包含垃圾值,这将编译。

它与以下内容相同:

{
    int len;
    len = len;
    printf("len %d", len);
}
于 2013-09-18T06:52:08.563 回答