Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"\n" or '\n' or std::endl to std::cout? [duplicate]

It was many years now since I stopped using std::endl to end lines when writing to std::cout, and started using "\n" instead.

But now I start seeing more snippets of code using '\n' instead, and I started wonder what might be best.

Besides the obvious that one is a string, and the other a character, is there any advantage to using this:

std::cout << variable << '\n';

Over this:

std::cout << variable << "\n";

Late addition:

When I asked this question I seemed to think that newline '\n' flushed the buffer. Now I know that it depends.

By default std::cin is tied to the old C stdin FILE* stream, and std::cout is tied to stdout. The flushing on newline comes from this tying. By default stdout, if connected to a terminal, is line-buffered. That means a new line will flush its buffers. So when printing a newline using std::cout, that will lead to stdout being flushed.

If stdout is not connected to a terminal (for example the output has been redirected or is piped), or if the tie between std::cout and stdout is broken, then newlines will not flush anything.

like image 980
Some programmer dude Avatar asked Nov 29 '11 12:11

Some programmer dude


People also ask

Should I use \n or Endl?

Both endl and \n serve the same purpose in C++ – they insert a new line. However, the key difference between them is that endl causes a flushing of the output buffer every time it is called, whereas \n does not.

Which is faster \n or Endl in C++?

The difference is obvious. The second one is much faster. std::endl always flush es the stream. In turn, \n simply puts a new line character to the stream, and in most cases this is exactly what we need.

What is std :: endl?

Inserts a newline character into the output sequence os and flushes it as if by calling os. put(os.

Can we use \n in C++?

The \n Character The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier. This is line one. This is line two.


5 Answers

Actually, '\n' should be the default. Unless you want to also explicitly flush the stream (and when and why would you want to do that?), there is no need to use std::endl at all.1
Of course, many books and tutorials use std::endl as the default. That is unfortunate and might lead to serious performance bugs.

I suppose there's little difference between using '\n' or using "\n", but the latter is an array of (two) characters, which has to be printed character by character, for which a loop has to be set up, which is more complex than outputting a single character. Of course, when doing IO this rarely matters, but if in doubt, when you want to output one character literal, output a character literal, rather than a whole string literal.
A nice side-effect of doing so is that you communicate in your code that you intended to output only a single character, and not just accidentally did this.


1Note that std::cout is tied to std::cin by default, which leads to std::cout being flushed before any input operation, so that any prompt will be printed before the user has to input something.

like image 110
sbi Avatar answered Sep 18 '22 11:09

sbi


There are no the best. You use what you need :

  • '\n' - to end the line
  • "some string\n" - the end the line after some string
  • std::endl - to end the line and flush the stream
like image 44
BЈовић Avatar answered Sep 19 '22 11:09

BЈовић


They do different things. "\n" Outputs a newline (in the appropriate platform-specific representation, so it generates a "\r\n" on Windows), but std::endl does the same and flushes the stream. Usually, you don't need to flush the stream immediately and it'll just cost you performance, so for the most part there's no reason to use std::endl.

like image 43
jalf Avatar answered Sep 22 '22 11:09

jalf


Edit: I worded my answer poorly, which may have lead people to believe that I thought "\n" actually printed a null character. This is of course wrong :)

Edit 2: Having looked at a C++ reference, chars are passed by reference anyway, so there's no difference there. The only difference is that the cstring will have to be searched for a delimiting character. The below isn't correct due to this fact.

'\n' would be ever so slightly more efficient than "\n", because the latter also contains a null character on the end, which means you're sending a char* to operator<<() (usually 4 bytes on a 32-bit system) as opposed to a single byte for a char.

In practice, this is borderline irrelevant. Personally, I follow the convention that Vladimir outlined.*

like image 44
Chris Parton Avatar answered Sep 22 '22 11:09

Chris Parton


  • std::cout << variable << std::endl;

    std::endl output a newline, but it also flushes the output stream. In other words, same effect as

    std::cout << variable << '\n'; // output newline
    std::cout.flush();      // then flush 
    
  • std::cout << variable << '\n';

    '\n' output a newline for a char, hence ostream& operator<< (ostream& os, char c); will be used.

  • std::cout << variable << "\n";

    "\n" is a const char[2], so ostream& operator<< (ostream& os, const char* s); will be used. We can imagine, this function will contain a loop, we might argue is overkill to just print out a newline.

like image 45
zangw Avatar answered Sep 22 '22 11:09

zangw