endl or \n ?

This is a discussion on endl or \n ? within the C++ Programming forums, part of the General Programming Boards category; I am wondering which is the difference between endl; and \n; . I saw in the tutorials of C++ that ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    66

    endl or \n ?

    I am wondering which is the difference between endl; and \n; .
    I saw in the tutorials of C++ that in the early example it uses

    Code:
    cout << "Some kind of mesage\n";
    but in the following sections (e.g. loops) it uses

    Code:
    cout << "some king of message" << endl;
    How do I know which is the most appropriate/suitable for any situations ?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It rarely matters much. Use whichever you prefer.

    Most often, the extra flush is unnecessary, so I would prefer '\n' over endl.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    What is a flush ?

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    there's an FAQ on that.

    In basic terms in "flushes" the output buffer.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I read the FAQ. So flush is fore clearing the input buffer. Ok guys, thank you all of you!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No, the output buffer. You cannot flush input streams.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Way to not properly read the FAQ. lol....

    You only interpretted it to mean the exact opposite of what it really says.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    So, could you write a simple simpe simple script that really shows the meaning of endl in order to understand the meaning of flush ?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    endl is typically implemented like this, ignoring wide characters and the template nature of iostreams:
    Code:
    ostream &endl(ostream &os)
    {
      os << '\n';
      os.flush();
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    First of all thank you very much. I do not understand this :P Could you write down something like those of tutorial C++ beginners script?

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Like stated \n and endl do the same thing they both move the cursor to the next line.

    endl flushes the output buffer.. I dont really see what else you need to know. \n is also an 'escape sequence' from a family with also \b backspace and \t tab
    I'm just trying to be a better person - My Name Is Earl

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    http://www.cprogramming.com/tutorial/lesson3.html

    Code:
    #include <iostream>
    
    using namespace std; // So the program can see cout and endl
    
    int main()
    {
      // The loop goes while x < 10, and x increases by one every loop
      for ( int x = 0; x < 10; x++ ) {
        // Keep in mind that the loop condition checks 
        //  the conditional statement before it loops again.
        //  consequently, when x equals 10 the loop breaks.
        // x is updated before the condition is checked.    
        cout<< x <<endl;
      }
      cin.get();
    }
    Why why why does he use entl ? There is nothing to flush, isn't it ?
    He can use /n instead. Ah ?

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    to be honest programmer use \n and endl out of choice. It really is a matter of personal taste, like Daved suggested. Some use endl and some use \n.

    C programmers cannot use endl and C doesnt use namespaces. So C programmers are proberly more renouned to using \n over endl out of tradition. It is a matter of personal choice.
    I'm just trying to be a better person - My Name Is Earl

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Could you give me an example that something need to be flushed ?
    Thank guys for all answerd btw!

Page 1 of 3 123 LastLast
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, 10: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, 12:05 AM

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