<<'\n' vs <<endl

This is a discussion on <<'\n' vs <<endl within the C++ Programming forums, part of the General Programming Boards category; Whats the difference between '\n' and endl ? i read somewhere that endl ends a line and flushes the stream ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    <<'\n' vs <<endl

    Whats the difference between '\n' and endl ?
    i read somewhere that endl ends a line and flushes the stream
    what does that mean?
    I AM WINNER!!!1!111oneoneomne

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it means that it flushes the buffer while making a new line. stuff such as values can be stored in the buffer, than can cause problems if not flushed. also, i believe endl is c++ while \n is c.

  3. #3
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Ahh i see. thanx.
    I AM WINNER!!!1!111oneoneomne

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    '\n' is the newline constant, it's used in both C and C++.

    I didn't know endl flushed the stream though.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    \n is used both in c and c++, and to clarify, endl is c++ only.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    An added information

    endl works this way

    first performing a "\n" operation
    an then calls flush() to flush the stream;
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    There was a large thread with a poll on this issue, you can search for it, and check it out...

    When you use endl it's same as:
    Code:
    cout << '\n' << flush;
    none...

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    '\n' might cause weirdness in Windows console

    I was just thinking about this this-morning on the way to work. So, I haven't tried using endl to solve my problem yet, but here's what happened:

    My little experimental program had 3 different cout statements all ending with '\n'. The program would wait 'till it got to the last one... Hmmm... Probably to the end of the program... then it would print the 3rd cout first, then #1, then #2. Weird Man!

    I knew it had something to do with the way the Windows OS intercepts and buffers output. I fixed the problem by switching to printf().

    I remember when I first tried-out the programs in the "21 days" book, I was typing end1 (end-one) instead of endl (end-ell). In the book the number 1 and letter l look the same. Hopefully they've changed the font in newer versions of the book.

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    copy cat!!!

    "\n" VS <<endl;

  10. #10
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    I was just working on a problem like this today...

    For some reason, in VC++.NET, anyhow, using endl over '\n' causes console output to screw up horribly. My friend had a perfect program he'd written, but it wouldn't output right, and the example he'd copied from (he was put in an AP C++ class without knowing C++ first...long story, don't ask) used '\n'. Long story short (oops, too late), he calls our new teacher over, and he can't figure it out, so the teacher calls me over, I causally glance at it, remember the same thing happening when I had a class last semester, made the changes, and it worked fine.

    I think I completely turned my teacher off to any microsoft compiler in the process, though. Now he won't stop *****ing out bit all...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tutorial confusion <<endl;
    By ikke in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2003, 09:39 AM
  2. "\n" VS <<endl;
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 11-16-2002, 03:48 AM

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