Home c++ Differences between ' n' and std :: endl

Differences between ‘\ n’ and std :: endl

Author

Date

Category

There is a code like this:

int main ()
{
  cout & lt; & lt; "Hello, world!" & lt; & lt; endl;
}

Second code:

int main ()
{
  cout & lt; & lt; "Hello, world! \ N";
}

I know endl does wrap and flush the buffer (flush ). But for some reason I don’t see the difference, that with endl , that with \ n , the result is the same – Hello, world!

is displayed

How do I rewrite the code to see the difference between endl and \ n ?


Answer 1, authority 100%

I think the problem is with output buffering.

In C++, input and output streams can be in one of three states: unbuffered (any output immediately goes to the target file / pipe / to the console), buffered line by line (characters get into the output after the end of each line, the exhaustion of the internal buffer or closing stream), and is fully buffered (characters appear in the output only after the internal buffer is exhausted or the stream is closed).

Microsoft Standard Library Implementation never buffers line by line (read the response from Stephan T . Lavavej ), Linux implementations are usually done.

this answer is a quote from the standard (C11 7.21.3 §7):

the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device

(this refers to pure C). C++ has the same policy, as per this answer :

C++ 11 27.4.2 [narrow.stream.objects] / 3 : The object cout controls output to a stream buffer associated with the object stdout .

This means that if the output is to the console, and not to a file, then you will always see the full line after \ n . But if the output is redirected, then buffering will occur.

Here’s an example. Consider the code:

# include "stdafx.h"
#include & lt; iostream & gt;
#include & lt; chrono & gt;
#include & lt; thread & gt;
int main ()
{
  std :: cout.sync_with_stdio (false);
  std :: cout & lt; & lt; "this is" & lt; & lt; "\ n";
  std :: this_thread :: sleep_for (std :: chrono :: seconds (10));
  std :: cout & lt; & lt; "SPARTAAAA !!! 111" & lt; & lt; std :: endl;
  return 0;
}

in MSVC 2015. If we run the program in the usual way, we will see first this is , and only after 10 seconds SPARTA . And if you run the program like this: a.exe & gt; x.txt , and look a couple of seconds after launching into the file x.txt , then you will not see anything there until the end the program works.

If you replace "\ n" with std :: endl , output to a file works the same as to the console.


Update: in MSVC 2015, buffering std :: cout can be set manually (idea with thanks stolen spied in this answer ):

std :: cout.sync_with_stdio (false);
char mybuf [1024];
std :: cout.rdbuf () - & gt; pubsetbuf (mybuf, 1024);
std :: cout & lt; & lt; "this is" & lt; & lt; "\ n";
std :: this_thread :: sleep_for (std :: chrono :: seconds (10));
std :: cout & lt; & lt; "SPARTAAAA !!! 111" & lt; & lt; std :: endl;
return 0;

This code only outputs text after std :: endl .


Answer 2, authority 18%

Draw something else to see the difference

int main ()
{
   cout & lt; & lt; "Hello, world1!" & lt; & lt; endl;
   cout & lt; & lt; "Hello, world2!" & lt; & lt; endl;
}

Will output:

Hello, world1!
Hello, world2!

But

int main ()
{
   cout & lt; & lt; "Hello, world1!";
   cout & lt; & lt; "Hello, world2!";
}

Will output:

Hello, world1! Hello, world2!

endl always flush flush the stream, flushing buffers. \ n just pushes a newline character into the stream. The difference is that \ n is faster

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions