为什么预处理器不将回车 return 字符视为白色 space 字符

Why the carriage return character is not considered as a white space character by the preprocessor

在 C 标准的 6.4 Lexical elements 部分写着

  1. ... Preprocessing tokens can be separated by white space; this consists of comments (described later), or white-space characters (space, horizontal tab, new-line, vertical tab, and form-feed), or both.

可以看出,回车 return 字符不包含在白色 space 字符的概念中。

另一方面,在标准 C 函数 isspace 的描述中写有 (7.4.1.10 The isspace function)

  1. ...The standard white-space characters are the following: space (''), form feed ('\f'), new-line ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). In the "C" locale, isspace returns true only for the standard white-space characters.

是否有意在描述预处理的部分中未提及回车 return 字符,如果是,原因是什么?

还是只是标准的缺陷?

同样的问题也适用于 C++ 标准。

参见 N1570 5.2.1 第 3 段。

回车 return 字符是基本执行字符集的成员(isspace() 将其视为白色-space 字符),但它不是基本源字符集。

源和执行基本字符集均包含"the space character, and control characters representing horizontal tab, vertical tab, and form feed"。另外,"In the basic execution character set, there shall be control characters representing alert, backspace, carriage return, and new line".

在某些系统上,回车 return 字符是行尾指示的一部分;任何此类指示都被视为单个换行符。源文件中 不是 行尾指示符一部分的回车 return 字符会导致未定义的行为。

源文件输入被翻译成源字符集(标准 §5.1.1.2 中的翻译阶段 1)。源字符集在§5.2.1中描述。

在 C.2011 中,§5.2.1¶3:

In source files, there shall be some way of indicating the end of each line of text; this International Standard treats such an end-of-line indicator as if it were a single new-line character.

空车return 不是源字符集的一部分。如果它作为行终止序列的一部分出现,它会在 C 预处理器开始工作之前被翻译成一个新行。