3

Possible Duplicate:
what does std::endl represent exactly on each platform?

I'm trying to figure out if std::endl returns \r\n, \n, or \r depending on the platform, or if it sticks with one all the time.

Community
  • 1
  • 1
Lakey
  • 1,948
  • 2
  • 17
  • 28
  • Thanks for that informational link. I did search for "value of std::endl" and that Q is not anywhere on the first 5 pages. I'm sorry about the duplicate question, but due to the fact that the term `std::endl` is so prolific, I'm sure you can understand how it might be hard to search for. – Lakey Jan 21 '13 at 20:07
  • 1
    No, no, I'm not pointing the accusing finger. The only way I got this result was using the word "platforms" alongside std::endl. I could easily see how one would fail to conjure up that particular phrasing. – PinkElephantsOnParade Jan 21 '13 at 20:09

2 Answers2

13

std::endl is just a "shorthand" for '\n' << std::flush. It is not platform dependent.

However, '\n' itself is handled differently on each platform and gets replaced with '\r\n', '\n', or '\r' (or something like that) if the stream is opened in text mode.

ipc
  • 8,045
  • 29
  • 33
  • 1
    Or something very different. If the system uses record oriented files, it might simply cause a new record to be started. Unless, of course, the file was opened in binary mode. – James Kanze Jan 21 '13 at 20:14
  • +1 I had no idea that `\n` had any special meaning to C++. I always thought it was simply `0x0A` according to the **LF** character on the ASCII table. – Lakey Jan 21 '13 at 20:27
  • Also std::endl flushes the buffer. `\n` does not flush the buffer which is a small performance benefit. – Skalli Jan 22 '13 at 14:27
2

'\n' Outputs a newline (in the appropriate platform-specific representation, so it generates a "\r\n" on Windows). std::endl does the same and flushes it.

Use '\n' instead of std::endl; when trying to output a newline, but use std::endl when trying to flush it. Unnecessary flushing decreases the performance of your application, for all we know file i/o is one of the slowest operations besides user i/o.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • +1 for when to use `\n` vs `endl` – Lakey Jan 21 '13 at 20:11
  • i think, if file i/o is a bottleneck, use something else than iostreams ;-) and just focus on readability. for that, `\n` may well be more readable than `endl`, but one problem is that ***it can be hidden inside strings***. so as a general convention i prefer `endl`. the flushing is just a nice side-effect, e.g. if a program crashes it's nice to have as much of the output as possible presented (and repeat, for efficiency use something other than iostreams, is my advice: efficiency is just an irrelevant argument here). – Cheers and hth. - Alf Jan 21 '13 at 20:12
  • Unless you know that it's going to cause problems, use `std::endl`. It sure makes things a lot easier to debug. – James Kanze Jan 21 '13 at 20:15
  • I was intrigued by this very question when I started learning C++ from "The C++ Programming Language" by Bjarne Stroustrup.. where he talks about `endl` and frequently uses `\n`. I first thought it could've been his habit from the C days, and I forced myself to actually ask him. This is his advice TBH. – Aniket Inge Jan 21 '13 at 20:16
  • For debugging purpose, one could also switch on line buffering. – ipc Jan 21 '13 at 20:17