end of line

May 14, 2014 at 6:54pm
This may sound dumb but I have the book call Jumping into C++ and I believe that the author is using the wrong syntax for the end of a line. I'm just learning C++ also and I know that I can use ( endl ) that's end L not one, I believe if you are to use an end of live by it self you can put it in "\n" in quotations.

The author uses '\n' is that proper in C++ or is that a carry over from C...?
May 14, 2014 at 7:13pm
It's ok to use '\n'. std::endl isn't quite the same thing. It's a stream manipulator that writes '\n' then flushes the stream.
May 14, 2014 at 7:24pm
Another subtlety, '\n' (with single quotes) is the new line character. "\n" (with double quotes) is actually a character array (or C-style string) that is 2 characters long containing the new line character and the null character.

Some functions, like the stream inserters, are overridden to handle both characters and character arrays. In that case, either will work.

1
2
3
std::cout << "Hi there" << '\n';
std::cout << "Hi there" << "\n";
std::cout << "Hi there" << std::endl;


The first 2 lines are essentially the same. The third line is equivalent to the first 2 plus it flushes std::cout so nothing remains buffered.
May 15, 2014 at 6:26am
Just to append to doug4;
in windows the newline is usually 0x0D, 0x0A hex.
Where in linux/unix its usually just 0x0A.
std:: is the namespace prefix if not.... using namespace std;
May 15, 2014 at 1:19pm
libc automatically handles translations from "\n" to whatever the platform's newline sequence is. Unless you are dealing with binary files directly, you don't have to worry about it. Just "\n" will do.

std::endl

is the same as

"\n" << std::flush
May 17, 2014 at 4:33pm
Thank You all for the replies.
Topic archived. No new replies allowed.