diff between [\n] and [endl]

am i correct in stating that \n means a new line
and that endl means end the line

so what are the differences between the two instructions?

for a complete beginner, could you say that they are almost identical instructions? if i just want to go on the next line, i could theoretically use either? thanks.
If you want to go to the next line, yes.

The basic difference is that '\n' is a character like 'k' or '\0' (it has the character value 10 or 13, I can never remember) while 'endl' is a stream manipulator (essentially a function, see http://www.cplusplus.com/reference/iostream/manipulators). endl, among other things, will flush the stream, i.e., empty it to whatever target it has (screen, file, ...).

So...if you just want to end the line you can do either. But if you use cout I suggest you stick with the manipulator cout since it will also flush your stream.
One point to note: If you are interested in faster screen output, using \n is faster because flushing the output takes a little time.

~psault
Most streams by default are line buffered which means they flush any time they see a \n, so in most cases there will not be a difference.

Try the following

1
2
3
4
5
int z;
cout << "Hello world";    // no \n or endl
cin >> z;                         // you won't see any output yet...
cout << "\n" << endl;    // Now you'll see Hello world
cin >> z; 


Then try the same thing with endl instead of \n and you'll get the same results.
thank you for the explanations.
Topic archived. No new replies allowed.