endl - \n

This is a discussion on endl - \n within the C++ Programming forums, part of the General Programming Boards category; sorry for posting so many posts here, but it is a C++ forum :-D just wondering, whats the different between ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    endl - \n

    sorry for posting so many posts here, but it is a C++ forum :-D

    just wondering, whats the different between \n and endl

    they both put it on a newline.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    endl will also flush the output stream which \n does not do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Except that endl will put a newline and also call flush() on the stream. For small programs it makes no difference, but that extra call does take some time. I generally prefer \n to endl. It's a preference.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    ok, so is it safe to use endl; everytime instead of \n or sometimes will u have to use \n?

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> ok, so is it safe to use endl; everytime instead of \n or sometimes will u have to use \n?

    Yes, I think. As citizen said, it does take a bit more time. Check this out:

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    double TimeLoop ( time_t Start, time_t End )
    {
    	double Difference = (double)( End - Start ) / 1000;
    
    	return Difference;
    }
    
    int main( void )
    {
    	time_t Start, End;
    	double TimeForENDL, TimeForSingle, TimeForDouble;
    
    	Start = clock();
    	for ( int i=0; i<100000; i++ )
    	{
    		cout<< endl;
    	}
    	End = clock();
    	TimeForENDL = TimeLoop( Start, End );
    
    
    
    	Start = clock();
    	for ( i=0; i<100000; i++ )
    	{
    		cout<< "\n";
    	}
    	End = clock();
    
    	TimeForDouble = TimeLoop( Start, End );
    
    
    
    	Start = clock();
    	for ( i=0; i<100000; i++ )
    	{
    		cout<< '\n';
    	}
    	End = clock();
    
    	TimeForSingle = TimeLoop( Start, End );
    
    	cout<< endl << "Time for endl: " << TimeForENDL
    		<< "\n" << "Time for double inverted comas: " << TimeForDouble
    		<< '\n' << "Time for double inverted comas: " << TimeForSingle
    		<< endl
    		<< "\n"
    		<< '\n';
    
    	return 0;
    }
    endl takes quite a bit longer as you can see. The others are fairly much the same
    Last edited by twomers; 05-25-2006 at 01:49 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sometimes {flush} it {flush} just {flush} doesn't {flush} make {flush} a {flush} whole {flush} lot {flush} of {flush} sense {flush} to {flush} be {flush} constantly {flush} flushing. {flush} {flush}

    Sometimes it's just easier to do it only when you need to. {flush}
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    lol ok cheers.

    thanks twomers that helped bud.

    thanks all. cheers. hugo.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    However...

    I would probably pay heed to how Dave worded his advice. He said "sometimes".

    The problem is that if your program crashes, all output left on the buffer will not be flushed (obviously). So you may get the wrong idea of where the program crashed. This can be especially tricky if the buffer happens to be filled within a control structure like a FOR loop and you only flush later, after leaving the for.

    Depening on the complexity of your stream manipulation code, I believe a good approach is to be quite lenient with buffer flushing techniques like the "endl" and "flush" manipulators during the development process. And once all is done and you are ready to do a release version of your code, review your code and delete all the extra manipulators paying then attention to dave's advise: flush only when you need too.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    ahh ok thats gave me a much better idea on it. thanks alot mate. i will look more into the manipulation thing a bit later on.

    cheers.
    Reece.

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, 02:50 PM
  2. endl
    By Mick D in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2005, 01:54 AM
  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, 01:42 PM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:05 AM

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