endl vs '\n'

This is a discussion on endl vs '\n' within the C++ Programming forums, part of the General Programming Boards category; I get that std::endl prints a newline character anyway but I heard it also flushes the buffer as well which ...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013

    endl vs '\n'

    I get that std::endl prints a newline character anyway but I heard it also flushes the buffer as well which means it's clearing all unused characters, correct?

    Like, a console would write characters to itself using a buffer as a source of input, right? Sorry, this is me logically assuming how things might work.

    This makes me more inclined to use endl over the newline character when printing using cout.

    But I've heard flushing the buffer for ever cout call can get expensive in terms of time.

    Should I just not care and use endl anyway?

    I saw it from a poster here, something like
    cout << "Hello, world!\n";
    /* instead of */
    cout << "Hello, world!" << endl;
    The first line is far easier to type, Imo, but I'm curious, what's the standard C++ way of using cout?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    When stdin is connected to the console it's line-buffered by default. So any time a newline is printed it will flush the buffer. Until the buffer is flushed, the user won't see anything, so it's usually what you want.

    I prefer '\n' over endl (as does Stroustrup, the inventor of C++).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2010
    When working strictly with the console it really doesn't matter which you use, the user and the console are much slower than either method. However when you start working with files you will begin to notice the difference in speed. But remember sometimes it is better to flush the output to insure data integrity.


  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    std::endl works simply by writing '\n' to the ostream and flushing the stream. As such, you'll want to use '\n' when you do not want your stream to be flushed. A couple of thoughts to keep in mind:

    • Under stringstreams, ostream.flush() does nothing. So std::endl doesn't have a performance impact there.
    • '\n' is made portable by the standard library, which converts it to the proper EOL character sequence. So it is not necessary to use std::endl to write portable code. Writing '\n' to the console or a filestream is good enough.

    Stream flushing won't really have any noticeable performance impact in your application, except perhaps when you are working with large buffers or files. But typically you should want to let the stream handle its own flushing and the only reason you should ever explicitly flush the stream is when you actually need it. A general rule is that you rarely ever need to explicitly flush a stream.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endl or \n ?
    By BlackSlash12 in forum C++ Programming
    Replies: 38
    Last Post: 08-26-2007, 05:17 AM
  2. endl or \n ?
    By plain in forum C++ Programming
    Replies: 5
    Last Post: 09-01-2006, 01:50 PM
  3. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 02:33 PM
  4. endl
    By Mick D in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2005, 12:54 AM
  5. std::endl;
    By kermit in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2005, 07:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21