Question:
I can't figure out why \n and endl act the same in C++, I thought they were different?
krohn211
2010-02-18 17:54:43 UTC
My understanding is that \n prints out a newline character. std::endl not only prints out a newline character but also flushes. My question is on the next 2 programs:

int main()
{
char *test = new char[10];
test[0] = 'a';
test[1] = 'b';
test[2] = 'n';
test[3] = 't';
test[4] = 'g';
test[5] = '\0';
cout << test;
while(1){

}
}

The above program prints out nothing because it is stuck in the infinite loop and doesn't do the cout. However, if I add an endl after test, it prints out. The odd thing is with this program:

int main()
{
char *test = new char[10];
test[0] = 'a';
test[1] = 'b';
test[2] = '\n'; //changed from n to \n
test[3] = 't';
test[4] = 'g';
test[5] = '\0';
cout << test;
while(1){

}
}

This program prints out ab and then stops. I thought \n didn't flush the buffer. Why is \n acting like endl? Shouldn't the last program print out nothing like the first program?
Four answers:
?
2010-02-19 06:35:14 UTC
If you've been searching the ISO standards, you should have found the answer.



ISO 14882 paragraph 27.6.2.7 Standard basic_ostream manipulators.



endl(basic_ostream& os) calls os.put(os.widen(ā€™\nā€™) ), then os.flush().



that's all it is. std::cout << std::endl; is the same as std::cout << "\n" << std::flush(); And, as others pointed out, what your terminal does to the characters later on is up to the OS.



Want to experience the difference?



#include

#include

using namespace std;



int main(){

ofstream null("/dev/null");

long i=0;

while(i<10000000L)

{

null << endl;

i++;

}

}



$ time ./test



real 0m2.183s

user 0m0.678s

sys 0m1.508s



#include

#include

using namespace std;



int main(){

ofstream null("/dev/null");

long i=0;

while(i<10000000L)

{

null << '\n';

i++;

}

}





$ time ./test



real 0m0.309s

user 0m0.304s

sys 0m0.000s



endl is 7 times slower than '\n', on my system in this test.
thebig_a_27
2010-02-18 18:02:32 UTC
this will probably vary from one system to another. anything that is in the buffer may be printed whether or not you manually flush. flushing just causes it to happen *now*



added:

\n is a newline character. it literally represents ascii value 10 on most systems today. it is no different than ascii value 65 ("A") or any other ascii value as far as C++ is concerned. There is no specification because there is nothing special about it. C++ just writes the value out like it would any other.





Now, your operating system or terminal driver may decide to flush output when it sees this character, but that's got *nothing* to do with C++.



Such is the world we live in. Different hardware made by different companies can work differently. Different operating systems may treat I/O differently. C++ specifies a minimum set of functionality so that some degree of cross platform compatibility can be obtained.



It is possible to achieve very precise control over your output, but this is considerably more complex than the simple routine you are using in your program. cout is rarely used outside of classroom exercises.
Ratchetr
2010-02-18 18:11:40 UTC
Vendor A guarantees that their widgets will pop if you fritz them.

Vendor B doesn't make that guarantee.



Therefore, Vendor B's widgets don't pop if you fritz them.



That's bad logic, of course, but that's what you are doing:



endl guarantees that it will flush the output buffer.

\n doesn't make that guarantee.



But \n might flush the output buffer, just like vendor B's widgets might pop if you fritz them.

Nobody said that can't happen.



You're writing to cout, where it's fairly standard to flush on an end of line.

If you changed your code to write to a file instead, you might get different results.

Or not.

Vendor B's widgets might pop when you fritz them.

Or not.
2016-12-26 15:42:55 UTC
neither is greater suitable, they the two have their makes use of. in my view i like the get away character /n. Why? by using fact it is uncomplicated to embed in literal strings...right it quite is occasion. cout << "n hi n my call is hal 900 n what's your call n"; could be making use of the endl output modifier... cout << endl <<" hi "<< endl << "my call is hal 900" << endl << "what's your call " << endl; it is only much less paintings.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...