-1
std::cout << "Enter two numbers:";
std::cout << std:endl;

This code snippet is followed by two paragraphs and a warning note, among which I understood the first para, but neither the second one nor the note. The text is as follows -

"The first output operator prints a message to the user. That message is a string literal, which is a sequence of characters enclosed in double quotation marks. The text between the quotation marks is printed to the standard output.

The second operator prints endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.

Warning Programmers often add print statements during debugging. Such statement should always flush the stream. Otherwise, if the program crashes, output may be left in the buffer, leading to incorrect inferences about where the program crashed."

So I didn't understand of the part of endl, nor the following warning. Can anyone please explain this to me as explicitly as possible and please try to keep it simple.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
daddyodevil
  • 184
  • 2
  • 13
  • what's your question? – xaxxon Feb 04 '17 at 12:25
  • The quoted part is directly from the book, what does that mean? – daddyodevil Feb 04 '17 at 12:26
  • what does what mean? The words are pretty straightforward. Which ones don't make sense in context? – xaxxon Feb 04 '17 at 12:27
  • Does this help? Output is not immediately written to the console, it is placed into an internal buffer until that buffer becomes full or is flushed. – Richard Critten Feb 04 '17 at 12:27
  • Personally I don't like that warning: the best tool to use when debugging is a debugger - not littering your code with a load of printf calls, which just might change the program behaviour. – Bathsheba Feb 04 '17 at 12:28
  • As you can see, all the different answers popping up are all guessing at what you are actually asking and people are coming to different conclusions... – xaxxon Feb 04 '17 at 12:29
  • 1
    @Bathsheba there is no "best tool". There are a bunch of approaches all of which have pros and cons depending on the situation. – xaxxon Feb 04 '17 at 12:29
  • I think you might find it a challenge to find a better tool than a debugger to do debugging. – Bathsheba Feb 04 '17 at 12:30
  • 2
    @xaxxon, if it was straightforward to me, I wouldn't be asking here. And all I wanted was for someone to explain me the text. I don't know what is ambiguous about it. And as for the answers here, I found each of them quite insightful and similar too. – daddyodevil Feb 04 '17 at 12:42
  • @Bathsheba "debugging" isn't a single type of behavior. If you have an issue that only happens infrequently, log files can be far more useful figuring out what is going on. For memory issues, things like address sanitizer or valgrind are far more useful. Each tool has its place. – xaxxon Feb 04 '17 at 12:47

3 Answers3

2

Imagine you have some code that crashes somewhere, and you don't know where. So you insert some print statements to narrow the problem down:

std::cout << "Before everything\n";

f1();

std::cout << "f1 done, now running f2\n";

f2();

std::cout << "all done\n";

Assuming that the program crashes during the evaluation of either f1() or f2(), you may not see any output, or you may see partial output that is misleading -- e.g. you could see only "Before everything", even though the crash happened in f2(). That's because the output data may be waiting in a buffer and hasn't actually been written to the output device.

The Primer's recommendation is therefore to flush each output, which you can conveniently achieve with endl:

std::cout << "Before everything" << std::endl;

f1();

std::cout << "f1 done, now running f2" << std::endl;

f2();

std::cout << "all done" << std::endl;

An alternative is to write debug output to std::cerr instead, which is not buffered by default (though you can always change the buffering of any ostream object later).

A more realistic use case is when you want to print a progress bar in a loop. Usually, a newline (\n) causes line-based output to be printed anyway, but if you want to print a single character for progress, you may not see it printed at all until after all the work is done unless you flush:

 for (int i = 0; i != N; ++i)
 {
     if (i % 1000 == 0)
     {
         std::cout << '#';   // progress marger
         std::cout.flush();
     }

     do_work();
 }

 std::cout << '\n';
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • So endl ensures everything prints before moving to the statement after endl, and /n only moves the cursor to the next line and what endl does is known as flushing. Thanks for your explanation, I think I got it. – daddyodevil Feb 04 '17 at 12:34
0

Well, simply:

std::cout << "Hello world!";

will print "Hello world!" and will remain in the same line. Now if you want to go to a new line, you should use:

std::cout << "\n";

or

std::cout << std::endl;

Now before I explain the difference, you have to know 1 more simple thing: When you issue a print command with the std::cout stream, things are not printed immediately. They are stored in a buffer, and at some point this buffer is flushed, either when the buffer is full, or when you force it to flush.

The first kind, \n, will not flush, but the second kind std::endl, will go to a new line + flush.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
0

Operating systems do buffered IO. That is, when your program outputs something, they dont necessarily put it immediately where it should go (i.e. disk, or the terminal), they might decide to keep the data in an internal memory buffer for some while before performing the actual IO operation on the device.

They do this to optmize performance, because doing the IO in chunks is better than doing it immediately as soon as there are a few bytes to write.

Flushing a buffer means asking the OS to perform immediately the IO operation without any more waiting. A programmer would do this this when (s)he knows that waiting for more data doesn't make sense.

The second note says that endl not only prints a newline, but also hints the cout to flush its buffer.

The 3rd note warns that debugging errors, if buffered and not flushed immediately, might not be seen if the program crashes while the error messages are still in the buffer (not flushed yet).

A.S.H
  • 29,101
  • 5
  • 23
  • 50