whats the difference

This is a discussion on whats the difference within the C++ Programming forums, part of the General Programming Boards category; whats the difference between \n and endl they both do the same thing?...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    whats the difference

    whats the difference between \n and endl they both do the same thing?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by lilhawk2892
    whats the difference between \n and endl they both do the same thing?
    endl prints a newline AND flushes the output stream.

    so you can do
    Code:
    for (int i = 0; i < 200; ++i)
    {
       cout << i << '\n';
       if (i == 66)
       {  
          // crash here
          ASSERT(false);
       }
    }
    and your output may or may not get to 66 depending on your systems buffering.
    but if you do
    Code:
    for (int i = 0; i < 200; ++i)
    {
       cout << i << endl;
       if (i == 66)
       {  
          // crash here
          ASSERT(false);
       }
    }
    you're guarenteed to get 0-66 printed out. Of course, flushing the buffer causes a slight performance penalty. Profile to find out if it affects you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Another way to put it

    endl is the same as this:
    Code:
    cout << "\n"; cout. flush();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 05:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:20 AM

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