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.
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 ...
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.
endl will also flush the output stream which \n does not do.
I used to be an adventurer like you... then I took an arrow to the knee.
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.
ok, so is it safe to use endl; everytime instead of \n or sometimes will u have to use \n?
>> 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:
endl takes quite a bit longer as you can see. The others are fairly much the sameCode:#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; }
Last edited by twomers; 05-25-2006 at 12:49 PM.
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.*
lol ok cheers.
thanks twomers that helped bud.
thanks all. cheers. hugo.
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.
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.