gpt4 book ai didi

编译器需要在第一列添加 # 吗?

转载 作者:行者123 更新时间:2023-11-30 14:54:24 25 4
gpt4 key购买 nike

是否存在广泛使用的 ANSI C 之前的编译器,要求 # 位于第一列?

† I would accept any compiler on this list. If I can find mention of it in the comp.lang.c Usenet newsgroup in a post dated before 1995, I would accept it.

K&R C 没有指定# 之前是否允许有空格。摘自《C 编程语言》原文,附录 A 中“C 引用手册”的第 12¶1 节:

The C compiler contains a preprocessor capable of macro substitution, conditional compilation, and inclusion of named files. Lines beginning with # communicate with this preprocessor.

因此,未指定 # 之前是否允许有空格。这意味着如果指令不是从第一列开始,则 ANSI 之前的编译器可能无法编译程序。

在 ISO C(以及之前的 ANSI C)中,C 预处理指令被明确允许以空格作为前缀。在 ANSI C (C-89) 中:

A preprocessing directive consists of a sequence of preprocessing tokens that begins with a # preprocessing token that is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character, and is ended by the next new-line character.

ISO C.2011 有类似的语言,但进一步澄清:

A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a # preprocessing token that (at the start of translation phase 4) is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character. The last token in the sequence is the first newline character that follows the first token in the sequence.165) A new-line character ends the preprocessing directive even if it occurs within what would otherwise be an invocation of a function-like macro.
165) Thus, preprocessing directives are commonly called ‘‘lines’’. These ‘‘lines’’ have no other syntactic significance, as all white space is equivalent except in certain situations during preprocessing (see the # character string literal creation operator in 6.10.3.2, for example).

最佳答案

简短回答:是的。

我记得写过这样的东西

#if foo
/* ... */
#else
#if bar
/* ... */
#else
#error "neither foo nor bar specified"
#endif
#endif

这样我曾经使用过的各种 pre-ANSI 编译器就不会提示“无法识别的预处理器指令‘#error’”。这可能是 Ritchie 最初的 pdp11 cc 或 pcc(“可移植 C 编译器”,IIRC,是 80 年代左右 Vax cc 的基础)。这两个编译器(更准确地说,是与这两个编译器一起使用的预处理器)肯定要求 # 位于第一列中。 (实际上,尽管这些编译器非常不同,但它们可能都使用了基本相同的预处理器的不同变体,而在当时预处理器始终是一个单独的程序。)

关于编译器需要在第一列添加 # 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779294/

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