什么是cout< “\\\<br/>”[a == N];做? [英] what does cout << "\n"[a==N]; do?
问题描述
在以下示例中:
cout<<\\\
[a == N]
我不知道 []
选项在 cout
中,但是当 a
的值等于<$ c时,不打印换行符$ c> N 。
[]选项在cout中执行
这实际上不是一个 cout
选项,发生的是\\\
是一个 字符串 。一个字符串字面量具有n const char 类型的数组, []
只是一个字符数组的索引,在这种情况下包含:
\\\
\0
注意 \0
附加到所有字符串文字。
code> == 运算符会生成 true 或 false ,因此索引为:
-
0
如果为false,如果a
code> N 导致\\\
-
/ code>如果为真,如果
a 等于
N
,则产生\0
这是相当神秘的,可以用一个简单的 / code>。
为了参考C ++ 14标准( Lightness确认草稿匹配实际标准)草稿为 2.14.5 $ c>中的 N3936 $ c>字符串文字 [lex.string] 说(强调我):
string literal 具有类型array of n const char,其中n是下面定义的字符串的
大小,并且具有静态存储持续时间
(3.7)。
和:
连接,在翻译阶段7(2.2)中,
'\0'附加到每个字符串文字,以便扫描字符串的程序可以找到它的结尾。
部分 4.5
[conv.prom]
类型bool的prvalue可以转换为int类型的prvalue,
false变为零,true成为一个。
向文本流写入空字符
声称在文本流中写入空字符( \0
)是未定义的行为。
因为我可以告诉这是一个合理的结论, cout
是根据C流定义的,我们可以从 27.4.2
[narrow.stream.objects] 其中说:
对象cout控制输出到与对象stdout相关联的流缓冲区,在
(27.9.2)中声明。
,只有在:数据只包括打印
字符和控件字符水平标签和换行符;
和打印字符< 7.4 字符处理< ctype.h> :
[... ]术语控制字符
指的是不打印
字符的特定于语言环境的字符集的成员。所有字母和数字都是打印字符。
与脚注 199
说:
在使用7位US ASCII字符集的实现中,打印字符是那些
,其值位于0x20(空格)到0x7E(波形);控制字符是
值从0(NUL)到0x1F(US)和字符0x7F(DEL)的那些。
,最后我们可以看到没有指定发送空字符的结果,我们可以看到 4
4中的未定义的行为:
[...]未定义的行为否则为
在本国际标准中由未定义的行为或[...]
我们还可以查看 C99 reasone 说:
< blockquote>
需要在文本流I / O中保留的字符集是编写C
程序所需的字符集;意图是标准应该允许C翻译器以最大的
便携式方式编写。控制字符(例如退格键)不需要用于此目的,因此它们在文本流中的
处理不是强制的。
In the following example:
cout<<"\n"[a==N];
I have no clue about what the []
option does in cout
, but it does not print a newline when the value of a
is equal to N
.
I have no clue about what the [] option does in cout
This is actually not a cout
option, what is happening is that "\n"
is a string literal. A string literal has the type array of n const char, the []
is simply an index into an array of characters which in this case contains:
\n\0
note \0
is appended to all string literals.
The ==
operator results in either true or false, so the index will be:
0
if false, ifa
does not equalN
resulting in\n
1
if true, ifa
equalsN
resulting in\0
This is rather cryptic and could have been replaced with a simple if
.
For reference the C++14 standard(Lightness confirmed the draft matches the actual standard) with the closest draft being N3936 in section 2.14.5
String literals [lex.string] says (emphasis mine):
string literal has type "array of n const char", where n is the size of the string as defined below, and has static storage duration (3.7).
and:
After any necessary concatenation, in translation phase 7 (2.2), ’\0’ is appended to every string literal so that programs that scan a string can find its end.
section 4.5
[conv.prom] says:
A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.
Writing a null character to a text stream
The claim was made that writing a null character(\0
) to a text stream is undefined behavior.
As far as I can tell this is a reasonable conclusion, cout
is defined in terms of C stream, as we can see from 27.4.2
[narrow.stream.objects] which says:
The object cout controls output to a stream buffer associated with the object stdout, declared in <cstdio> (27.9.2).
and the C11 draft standard in section 7.21.2
Streams says:
[...]Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line;
and printing characters are covered in 7.4
Character handling <ctype.h>:
[...]the term control character refers to a member of a locale-specific set of characters that are not printing characters.199) All letters and digits are printing characters.
with footnote 199
saying:
In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).
and finally we can see that the result of sending a null character is not specified and we can see this is undefined behavior from section 4
Conformance which says:
[...]Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior.[...]
We can also look to the C99 rationale which says:
The set of characters required to be preserved in text stream I/O are those needed for writing C programs; the intent is that the Standard should permit a C translator to be written in a maximally portable fashion. Control characters such as backspace are not required for this purpose, so their handling in text streams is not mandated.
这篇关于什么是cout< “\\\<br/>”[a == N];做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!