C ++标准(源文件的换行符结尾) [英] C++ Standards (newline ending of source files)
问题描述
我指的是:为什么文本文件应该以换行符结尾? 答案之一是引用C89标准.简而言之,该命令指示文件必须以换行符结尾,并且不能紧接反斜杠.
I am referring to: Why should text files end with a newline? One of the answers quotes the C89 standard. Which in brief dictates that a file must end with a new line, which is not immediately preceded by a backslash.
这是否适用于最新的C ++标准?
Does that apply to the most recent C++ standard?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
//\
以上有效吗? (假设///之后有一个换行符,我一直无法显示它)
Is the above valid? (Assuming there is a newline after //\, which I've been unable to display)
推荐答案
对于C ++,给定的代码是合法的,但对于C语言则不是.
The given code is legal in the case of C++, but not for C.
事实上,C(N1570)标准说:
Indeed, the C (N1570) standard says:
每个反斜杠字符(
\
)的实例均紧跟换行符 字符被删除,将物理源线拼接起来以形成逻辑源线. 仅任何物理源代码行上的最后一个反斜杠才有资格参与 这样的接头.不为空的源文件应以换行符结尾, 在任何此类字符之前,不得立即在其后加上反斜杠字符 进行拼接.
Each instance of a backslash character (
\
) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.
C ++标准(N3797)的表达方式有所不同(强调我的意思):
The C++ standard (N3797) formulates it a bit differently (emphasis mine):
每个紧跟换行符的反斜杠字符(
\
)的每个实例都将被删除, 拼接物理源线以形成逻辑源线.只有任何物理上的最后一个反斜杠 源代码行有资格成为此类接头的一部分.结果是,如果一个字符序列 匹配产生通用字符名称的语法,该行为未定义. 源文件 不为空且不以换行符结尾或以换行符结尾的字符 在进行任何此类拼接之前,应紧接在反斜杠字符之后,然后进行处理 就像在文件中附加了换行符一样.
Each instance of a backslash character (
\
) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.
这篇关于C ++标准(源文件的换行符结尾)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!