"\n" vs. '\n'

J

Juha Nieminen

I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".

What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?

Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?

The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)
If you repeatedly call std::getline(is, str, '\n') in a system where
newlines actually consist of more than one character, aren't the
additional characters read into the string (even though they really
shouldn't) instead of skipped? There doesn't seem to be a version of
std::getline() taking a string as delimiter parameter, so you can't give
it a "\n".
 
P

peter koch

I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".

What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?

Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?

The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)
If you repeatedly call std::getline(is, str, '\n') in a system where
newlines actually consist of more than one character, aren't the
additional characters read into the string (even though they really
shouldn't) instead of skipped? There doesn't seem to be a version of
std::getline() taking a string as delimiter parameter, so you can't give
it a "\n".

For the user endline is \n and always one character. An operating
systems may encode endline as one character, two characters or no
character at all (I do not know of any other way to do it, but there
could possibly be more). It is the job of the io machinery to
translate these representations into a single endline character unless
you choose to open the stream/file in binary mode.

/Peter
 
P

Pete Becker

I assume that using '\n' to print a newline is not portable because in
some systems newline consists actually of two characters and '\n' is
only one. Thus the only portable way of printing a newline character
(for example with std::printf) is "\n".

'\n' is a character. "\n" is a string consisting of two characters:
'\n' and '\0'.
What does the standard say about '\n'? Can any assumptions be made
about it? Or is it a "never use it in portable code" value?

'\n' is a newline character. When it's written to a text stream the
runtime library will do whatever is appropriate for the system the
program was compiled for. These both have the same effect:

printf("\n");
printf("%c", '\n');
Of course this might present a problem when reading an input file in a
byte-by-byte fashion. If you want to detect a newline while reading it
like that, can you simply compare a read byte with '\n'? What if a
newline actually consists of more than one character?

If you're reading in text mode, the system's end-of-line representation
will be translated to the character '\n'. If you're reading in binary
mode you get whatever bytes are in the file.
The std::getline() function accepts a delimiter character as third
parameter (the input is read until this character appears). Since the
type of this parameter is char, doesn't this actually present a problem
if you want to give it a newline as delimiter? (I know that newline is
the default delimiter, but in theory it might be possible that you might
want to be able to specify it explicitly...)

If you're calling getline() you'd better be using a text stream. The
system's end-of-line representation will be translated to the character
'\n'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top