-
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 ?
-
-
It rarely matters much. Use whichever you prefer.
Most often, the extra flush is unnecessary, so I would prefer '\n' over endl.
-
-
there's an FAQ on that.
In basic terms in "flushes" the output buffer.
-
I read the FAQ. So flush is fore clearing the input buffer. Ok guys, thank you all of you!
-
No, the output buffer. You cannot flush input streams.
-
Way to not properly read the FAQ. lol....
You only interpretted it to mean the exact opposite of what it really says.
-
So, could you write a simple simpe simple script that really shows the meaning of endl in order to understand the meaning of flush ?
-
endl is typically implemented like this, ignoring wide characters and the template nature of iostreams:
Code:
ostream &endl(ostream &os)
{
os << '\n';
os.flush();
}
-
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?
-
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
-
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 ?
-
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.
-
Could you give me an example that something need to be flushed ?
Thank guys for all answerd btw!