Monday, 22 September 2014

Difference between endl and '\n'


Look at these two output statements:

cout << endl << "This is output.";
cout << endl << "\tThis is output after a tab.";

They will produce these lines:

This is output.
This is output after a tab.

The \t in the second output statement causes the output text to be indented to the first tab position.

In fact, instead of using endl, you could use the escape sequence for the newline character, \n, in each string, so you could rewrite the preceding statements as follows:

cout << "\nThis is output.";
cout << "\n\tThis is output after a tab.";

Note that \n is not quite the same as endl. endl will output a newline and flush the stream, while \n will output the newline without flushing the stream.

No comments:

Post a Comment