c - 根据 C 标准,带参数的宏可能会与标识符发生冲突吗?

标签 c

我认为最好用一个例子来说明。使用这样的结构:

typedef struct {
    int a;
    char b;
} Foo;

定义一个宏:

#define Foo(A, B) (Foo){ A, B }

然后可以这样使用:

Foo foo;
foo = Foo(1, 'c');

我已经使用这种模式一段时间了,对我来说,这是一种非常优雅的结构初始化方式。它适用于 GCC 和 Clang(我认为还有 TCC),但在一些鲜为人知的 C 编译器(如 Plan 9 的 C 编译器)中它不起作用。

C 标准对这个主题有何规定?

最佳答案

摘自 C11 标准的 §6.10.3.10:

Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).

所以,不,你不应该担心。只有在宏名称后跟左括号 (.

关于c - 根据 C 标准,带参数的宏可能会与标识符发生冲突吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787755/

相关文章:

c - 如何使用strspn查找strspn的arg 2中提到的字符数以外的字符数?

C语言: Error in implementing binary search tree using structures

c++ - 进程C的动态池

c - linux内核模块中的最大指针/数组大小

c - 是否有基于 Microsoft C 的列表集合?

c - 多线程和缓存非同时使用内存

c - 如何在 fgets 循环中正确刷新标准输入

c - fgets 将字符串添加到另一个变量

c - 是scanf (“%4s%4s”,b,b);定义明确? [复制]

c - 将结构中的结构传递给 GSourceFunc 的最佳方法