是否可以重新定义“。”在 C 中使用宏?
Is it possible to redefine "." using macros in C?
我有一个关于 C 预处理器的(可能是错误的)学校作业,我应该在其中定义一个允许
的宏
Today is the 9.
编译为
int a = 9;
注意“.” 9之后,程序的其余部分类似,我对此没有问题。
现在我用 int
(#define Today int
) 替换了“今天”,用 a
替换了“是”,用 =
替换了“the”,但我不知道如果我只是盲目地通过做
来替换它,那么如何处理“。”
#define . ;
我遇到编译时错误。
甚至可以用点做点什么吗?
#define Today int
#define is a
#define the = (int)
void foo(void)
{
Today is the 9.;
printf("%d\n", is);
}
Is it possible to redefine "." using macros in C?
没有
given if I just blindly replace it by doing
#define . ;
I get a compile-time error. Is it even possible to do something with
the dot?
不,不可能。
首先,根据 C 的规则,文本中的 .
不是一个单独的标记。它是 9.
的一部分,一个浮点常量。宏替换仅对完整的标记进行操作。
其次,宏替换不是一般的搜索/替换。宏名称必须是 C 标识符,以下划线或拉丁字母开头,并且仅包含下划线、拉丁字母和十进制数字。因此,无法单独定义 .
或将完整的 9.
定义为宏名称。
我有一个关于 C 预处理器的(可能是错误的)学校作业,我应该在其中定义一个允许
的宏Today is the 9.
编译为
int a = 9;
注意“.” 9之后,程序的其余部分类似,我对此没有问题。
现在我用 int
(#define Today int
) 替换了“今天”,用 a
替换了“是”,用 =
替换了“the”,但我不知道如果我只是盲目地通过做
#define . ;
我遇到编译时错误。 甚至可以用点做点什么吗?
#define Today int
#define is a
#define the = (int)
void foo(void)
{
Today is the 9.;
printf("%d\n", is);
}
Is it possible to redefine "." using macros in C?
没有
given if I just blindly replace it by doing
#define . ;
I get a compile-time error. Is it even possible to do something with the dot?
不,不可能。
首先,根据 C 的规则,文本中的 .
不是一个单独的标记。它是 9.
的一部分,一个浮点常量。宏替换仅对完整的标记进行操作。
其次,宏替换不是一般的搜索/替换。宏名称必须是 C 标识符,以下划线或拉丁字母开头,并且仅包含下划线、拉丁字母和十进制数字。因此,无法单独定义 .
或将完整的 9.
定义为宏名称。