Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does cout << "\n"[a==N]; do?

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.

like image 680
Siva Prasad Avatar asked Jun 17 '15 10:06

Siva Prasad


People also ask

What does cout << \n Do?

cout << endl inserts a new line and flushes the stream(output buffer), whereas cout << “\n” just inserts a new line.

Why Cout is used in C++?

The cout object is used to display the output to the standard output device. It is defined in the iostream header file.

What is the meaning of cout << str at 5?

It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

What does << mean in C++?

MiiNiPaa (8886) << is a bitwise left shift operator. It is overloaded to work differently with ostream and derived classes. Standard library provides overloads fo all built-in types and several calsses from standard library (std::string for example). You can also provide overload for your own classes.


1 Answers

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, if a does not equal N resulting in \n
  • 1 if true, if a equals N 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.

like image 115
Shafik Yaghmour Avatar answered Oct 11 '22 21:10

Shafik Yaghmour