I've written a program to calculate RAM usage on a Linux machine, and I've experienced some strange behavior trying to output the results. The code is as follows:
And this works fine. However, I want to change the output so it's on one line, which I believe would be done through the /r escape character. I had a go, and found that it didn't work. Upon looking further, I found that removing \n from line 25 makes the program print nothing, it just sits in the loop forever. This happened even when there were no \r or any other escape characters in the program. Adding it back (or endl) and it printed again.
The output stream is buffered. That makes a lot of sense with for example a disk file, instead of writing one character at a time, the data is cached in a buffer until it is full, then the whole lot is written at once.
In the case of output to the console, the endl code automatically flushes the buffer (that is, forces it to write out the contents, even if the buffer is not full). In the case of the newline character '\n', I wasn't quite sure, I know this can sometimes force the emptying of the buffer too.