gpt4 book ai didi

c - 使用预处理器宏插入单行注释

转载 作者:行者123 更新时间:2023-12-02 08:29:23 26 4
gpt4 key购买 nike

是否可以使用预处理器宏(或魔术)模拟单行注释(//)?例如,这可以用 gcc -std=c99 编译吗?

#define LINE_COMMENT() ???

int main() {
LINE_COMMENT() asd(*&#@)($*?><?><":}{)(@
return 0;
}

最佳答案

没有。以下是标准的摘录,显示了 C 程序的翻译阶段:

  1. The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.

  2. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. If a character sequence that matches the syntax of a universal character name is produced by token concatenation (6.10.3.3), the behavior is undefined. A #include preprocessing directive causes the named header or source file to be processed from phase 1 through phase 4, recursively. All preprocessing directives are then deleted.

如您所见,注释在宏扩展之前被删除,因此宏不能扩展为注释。

您显然可以定义一个带有参数并展开为空的宏,但它比注释稍微严格一些,因为它的参数必须仅包含有效的预处理器标记字符(例如,没有 @ 或无与伦比的报价)。对于一般评论目的不是很有用。

关于c - 使用预处理器宏插入单行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28725780/

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