Whats the difference between '\n' and endl ?
i read somewhere that endl ends a line and flushes the stream
what does that mean?
Printable View
Whats the difference between '\n' and endl ?
i read somewhere that endl ends a line and flushes the stream
what does that mean?
it means that it flushes the buffer while making a new line. stuff such as values can be stored in the buffer, than can cause problems if not flushed. also, i believe endl is c++ while \n is c.
Ahh i see. thanx.
'\n' is the newline constant, it's used in both C and C++.
I didn't know endl flushed the stream though.
\n is used both in c and c++, and to clarify, endl is c++ only.
endl works this way
first performing a "\n" operation
an then calls flush() to flush the stream;
There was a large thread with a poll on this issue, you can search for it, and check it out...
When you use endl it's same as:
Code:cout << '\n' << flush;
I was just thinking about this this-morning on the way to work. So, I haven't tried using endl to solve my problem yet, but here's what happened:
My little experimental program had 3 different cout statements all ending with '\n'. The program would wait 'till it got to the last one... Hmmm... Probably to the end of the program... then it would print the 3rd cout first, then #1, then #2. Weird Man!
I knew it had something to do with the way the Windows OS intercepts and buffers output. I fixed the problem by switching to printf().
:) I remember when I first tried-out the programs in the "21 days" book, I was typing end1 (end-one) instead of endl (end-ell). In the book the number 1 and letter l look the same. Hopefully they've changed the font in newer versions of the book.
I was just working on a problem like this today...
For some reason, in VC++.NET, anyhow, using endl over '\n' causes console output to screw up horribly. My friend had a perfect program he'd written, but it wouldn't output right, and the example he'd copied from (he was put in an AP C++ class without knowing C++ first...long story, don't ask) used '\n'. Long story short (oops, too late), he calls our new teacher over, and he can't figure it out, so the teacher calls me over, I causally glance at it, remember the same thing happening when I had a class last semester, made the changes, and it worked fine.
I think I completely turned my teacher off to any microsoft compiler in the process, though. Now he won't stop *****ing out bit all...