简体   繁体   English

为什么回车符不被预处理器视为空白字符

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

In the section 6.4 Lexical elements of the C Standard there is written在 C 标准的第6.4 Lexical elements6.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.这由注释(稍后描述)或空白字符(空格、水平制表符、换行符、垂直制表符和换页符)或两者组成。

As it is seen the carriage return character is not included in the notion of the white space characters.正如所见,回车符不包含在空白字符的概念中。

On the other hand in the description of the standard C function isspace there is written ( 7.4.1.10 The isspace function )另一方面,在标准 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'). ...标准的空白字符如下:空格 ('')、换页符 ('\\f')、换行符 ('\\n')、回车 ('\\r') 、水平制表符 ( '\\t') 和垂直制表符 ('\\v')。 In the "C" locale, isspace returns true only for the standard white-space characters.在“C”语言环境中, isspace 仅对标准空白字符返回 true。

Is it intentionally that the carriage return character is not mentioned in the section describing preprocessing and if so what is the reason?是否有意在描述预处理的部分中未提及回车符,如果是,原因是什么?

Or is it just a Standard's defect?或者这只是标准的缺陷?

The same questions are valid for the C++ Standard.相同的问题适用于 C++ 标准。

See N1570 5.2.1 paragraph 3.参见N1570 5.2.1 第 3 段。

The carriage return character is a member of the basic execution character set (and it treated by isspace() as a white-space character), but it's not part of the basic source character set.回车符是基本执行字符集的成员(并且它被isspace()视为空白字符),但它不是基本源字符集的一部分。

The source and execution basic character sets both include "the space character, and control characters representing horizontal tab, vertical tab, and form feed".源和执行基本字符集都包括“空格字符,以及表示水平制表符、垂直制表符和换页符的控制字符”。 In addition, "In the basic execution character set, there shall be control characters representing alert, backspace, carriage return, and new line".另外,“在基本执行字符集中,应该有代表警告、退格、回车和换行的控制字符”。

On some systems, the carriage return character is part of the indication of an end-of-line;在某些系统上,回车符是行尾指示的一部分; any such indication is treated as a single new-line.任何此类指示都被视为单个换行符。 A carriage return character that's not part of an end-of-line indicator in a source file causes undefined behavior.不是源文件中行尾指示符的一部分的回车字符会导致未定义的行为。

The source file input gets translated into the source character set (translation phase 1 in §5.1.1.2 of the standard).源文件输入被翻译成源字符集(标准第 5.1.1.2 节中的翻译阶段 1)。 The source character set is described in §5.2.1.源字符集在§5.2.1 中描述。

In C.2011, §5.2.1¶3:在 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.本国际标准将这样的行尾指示符视为单个换行符。

A bare carriage return is not part of the source character set.裸回车不是源字符集的一部分。 If it appears as part of a line termination sequence, it gets translated into a single new-line before the C preprocessor begins to do its work.如果它作为行终止序列的一部分出现,它会在 C 预处理器开始工作之前被翻译成一个单独的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM