2

Before you declare this question as a duplicate of this or that, please consider that I've submitted a problem to the online judge with \n and got WA, and then with std::endl and got AC. So, I need a very specific answer to the point of platform awareness: Is \n really platform aware, and does the runtime really write the correct line ending according to the platform as the answers to the other questions claim? If so, could you please tell me how this happened??

If the answer can be supported by a citation from the standard regarding the platform awareness issue, I'd be really thankful. I've read all the other questions' answers (even the closed ones), so please don't repeat the "flushes the buffer" thing.

Community
  • 1
  • 1
OmarOthman
  • 1,718
  • 2
  • 19
  • 36
  • WA = Wrong Answer, AC = Accepted. This means - obviously - that there was a difference in the file output on the judge's server. – OmarOthman Mar 14 '12 at 09:46
  • Your answer will probably be accepted if you use \n and then do `std::cout << std::flush` at the end. Did you try that? – Joseph Mansfield Mar 14 '12 at 09:59
  • So what was the problem? This distinction may indicate more of a problem on the judge's side than in your code. (Or it may not: there are cases where `std::endl` or `'\n'` followed by a flush is the only right answer. And if the problem involved outputting a large body of data in a single go, with performance an issue, then it could be argued that `std::endl` is the wrong answer, although this is rarer, and constitutes more of a special case.) – James Kanze Mar 14 '12 at 10:04
  • @sftrabbit If he closes the stream at the end, that's fine too. If he doesn't check the status after the close, and reflect it in his return code, however, that is unacceptable. – James Kanze Mar 14 '12 at 10:07
  • [@sftrabbit] I've tried that as per your suggestion, but it failed. I've tried flushing the buffer at the end and clearing it at the end, but in vain. – OmarOthman Mar 14 '12 at 10:20
  • [@JamesKanze] If this is the case, then the output should be TLE, not WA. Note that `std::endl` - the slower one - is the one that got accepted, though! – OmarOthman Mar 18 '12 at 06:09

2 Answers2

6

From C++11, §27.7.3.8 -

namespace std
{
    template <class charT, class traits>
        basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
}

Effects:Calls os.put(os.widen(’\n’)), then os.flush().

Returns:os.

So, from the standard it's clear that endl flushes the output stream buffer, while \n doesn't. Generally you will want to use endl for printing a new line, but you should also keep in mind that every time you do that the output buffer will get flushed too.

About the platform awareness

From the standard it's obvious that both does the exact same thing - printing a new line in the exact same way. So, if one is platform independent, then the other should also be the same. Since I know for sure endl is platform independent, same should be the case for \n.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • A bit of a nit, but `endl` doesn't "clear" the output stream, it flushes it. Within the standard, at least, "clear"ing a stream means resetting its error bits, not flushing it. – James Kanze Mar 14 '12 at 10:06
  • @JamesKanze: Yeah, that's what I tried to mean, just used the wrong word :-). It's fixed now :-) . – MD Sayem Ahmed Mar 14 '12 at 10:08
  • 1
    Annoyingly, it's 27.7.2.8 in N3092 (final version before official standard) and 27.7.3.8 in N3337 (first version after) because the Overview doesn't get a number in the final draft. I don't have the actual standard, so this was a pain to find... Oh well, have to accept it or pay for the proper version. – BoBTFish Mar 14 '12 at 11:35
5

std::endl is a manipulator which outputs '\n', then flushes the stream. For most casual use, it's what you should be using. When outputting large volumes of data, however, it can result in a significant performance hit, in which case, use '\n'.

EDIT:

With regards to platform awareness: both output exactly the same thing. If the stream was opened in text mode, the '\n' will cause a platform dependent representation of a line ending to occur: 0x0A under Unix, 0x0D, 0x0A under Windows, start a new record under some mainframes, etc. If the stream was opened in binary mode, it will cause a single byte (typically 0x0A, but it actually depends on the basic character encoding used) to be output.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • In response to your edit, I've written the output of both programs (using endl and \n) to two files, and compared them using KDiff3. It declares that both are *binary equal*! I really don't know what's happening... that I've started to really suspect the problem to be from the judge's side!! – OmarOthman Mar 14 '12 at 10:27
  • Whether you use `'\n'` or `std::endl` has absolutely no impact on the contents which are output. The only difference is the flush: the `'\n'` and any preceding output will appear immediately with `std::endl`; it will (probably) appear much later with `'\n'`---or never, if the program crashes in the meantime. (This is the reason why one should prefer `std::endl` for most "casual" writes.) – James Kanze Mar 14 '12 at 10:38