Thread: endl

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    5

    endl

    hi, sorry if this is too noob a question but what exactly does endl do? i notice it used alot at the end of cout but it isnt always used

    thanks for any help

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    endl places a newline in the buffer and then flushes the buffer.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    5
    ah i see, cool thanks.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I suggest that you use "\r\n" or "\n" instead of endl, it's cleaner and shorter.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I suggest that you use "\r\n" or "\n" instead of endl, it's cleaner and shorter.
    But they don't guarantee that the output stream will be flushed, unlike endl.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    endl has it's place. for example, take a look at this:
    Code:
    /* This */
    std::cout<<"lorem ipsum dolor sit amet\n"
    	<<"consectetuer adipiscing elit.\n"
    	<<"Integer vel nibh eu mi iaculis facilisis."<<std::endl;
    
    /* Is faster than this */
    std::cout<<"lorem ipsum dolor sit amet"<<std::endl
    	<<"consectetuer adipiscing elit."<<std::endl
    	<<"Integer vel nibh eu mi iaculis facilisis."<<std::endl;
    
    /* Which is faster than this */
    std::cout<<"lorem ipsum dolor sit amet"<<std::endl
    std::cout<<"consectetuer adipiscing elit."<<std::endl
    std::cout<<"Integer vel nibh eu mi iaculis facilisis."<<std::endl;
    in that example, the speed won't be noticable, but if you have alot of output, you'll definately notice the difference. The first example is the most efficient of the three because there's no need to flush the buffer every line, unless you're worried about some kind of error occurring in the middle of that block that would halt output. In that case, you should probably be using unbuffered (cerr) output.
    Last edited by major_small; 10-19-2005 at 11:06 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    thank you very much ,
    major_small

    That was a new piece of information for me!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You should worry about getting it working before trying to make it faster/smaller
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endl or \n ?
    By plain in forum C++ Programming
    Replies: 5
    Last Post: 09-01-2006, 01:50 PM
  2. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 02:33 PM
  3. Endl - Undeclared identifier.
    By MipZhaP in forum C++ Programming
    Replies: 9
    Last Post: 03-03-2005, 11:01 AM
  4. endl vs \n
    By Chewbacca in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 12:42 PM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:05 AM