gpt4 book ai didi

c - #define 多次没有#undef,合法吗?

转载 作者:太空宇宙 更新时间:2023-11-04 00:11:35 27 4
gpt4 key购买 nike

比如我定义了3次AA,合法吗?:

#include<stdio.h>
#define AA 10
#define AA 20
#define AA 30
int main() {
printf("AA");
}

最佳答案

这在 C 和 C++ 中都是不合法的。

引自 C 标准草案 N1570 :

6.10.3 Macro replacement

Constraints

1 Tw o replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

2 An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical.

引自 C++ 标准草案 N4582 :

16.3 Macro replacement [cpp.replace]

1 Two replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

2 An identifier currently defined as an object-like macro may be redefined by another #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical, otherwise the program is ill-formed. Likewise, an identifier currently defined as a function-like macro may be redefined by another #define preprocessing directive provided that the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical, otherwise the program is ill-formed.

关于c - #define 多次没有#undef,合法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36976550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com