whats the difference between /n and endl?
Printable View
whats the difference between /n and endl?
nothing
all it does is prevent extra shift keying
cout << somevariable << endl;
instead of
cout << somevariable << "\n";
\n is an escape sequence.
And endl does the same thing but its a standatd strem thingie.
I really dont know the technicalities. Im sure someone does.
example
Code:cout << "Line one\nLine two";
cout << "Line one" << endl << "Line Two";
ok, there is a difference....
endl is a global function which returns '\n'
<< is an overloaded operator which tells the ostream class cout how to handle the data type that follows
i believe endl also calls flush to flush the stream
ok....when did a stream come into the picture?
endl is a manipulator in the ostream class. When inserted into an output stream it inserts a newline character then flushes the stream. It is cheaper to just use "\n" then to use endl because flushing the stream unnecessarily can slow down your program.
a stream came into the picutre you you put
#include <iostream>
in your program
Actually I believe NiVaG was referring to your seemingly irrelevant explanation of the insertion operator.Quote:
a stream came into the picutre you you put
#include <iostream>
in your program
when using \n be sure to use single quotes to insert the character. When you use double quotes it creates a null terminated c-string which will cost you an extra sizeof(char) ;)Quote:
Originally Posted by misplaced
Here is a sample implementation of endl:
When talking about endl, you are implying streams as endl is an operator to ostream (a stream class). So there is definitely relevance there, adn as stated before endl does a flush() after "\n".Code:template<class _Elem, class _Traits> inline
basic_ostream<_Elem, _Traits>&
__cdecl endl(basic_ostream<_Elem, _Traits>& _Ostr)
{ // insert newline and flush stream
_Ostr.put(_Ostr.widen('\n'));
_Ostr.flush();
return (_Ostr);
}
Sometimes when I use cin.get() to pause the program before ending (keep the console open long enough to read it) it doesn't work by itself and I have to precede it with cin.ignore(). I understand that this is because there is still a \n in the input buffer....maybe? Would using endl, since it flushes the stream, make it so that I wouldn't have to enter cin.ignore()? Or are they completely unrelated things?
Couldn't you use cout.flush() and cin.flush() before cin.get()?Quote:
Originally Posted by Elhaz
I don' know. That's the first time I've seen those. I'll check them out. Thanks.Quote:
Couldn't you use cout.flush() and cin.flush() before cin.get()?
Since endl flushes the output stream, it wouldn't help much in clearing the input stream.
I believe flush is only defined for output streams, so it wouldn't work for cin.
There is no flush() for istream.
Thanks jlou. For some strange reason I had the two streams confused in my mind (going into the input buffer then waiting, then coming out of the input buffer when cin.get() was called... "out of", ah! output!... silly :o )Quote:
Since endl flushes the output stream, it wouldn't help much in clearing the input stream.