K&R2 2.3 : /n vs /r

M

Merrill & Michele

Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Q2) What on earth is the difference between newline and carriage return?

MPJ
 
J

john blackburn

Merrill said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Q2) What on earth is the difference between newline and carriage return?

MPJ

'\r' is simply a carriage return whereas '\n' is a carriage return followed
by line feed. Note that a carriage return is simply that, it moves the
cursor to the beginning of the current line and does not move on to the
next line.

It is here you have to be careful as UNIX text files are of a different
format from Windows text files and need conversion when moving from one o/s
to the other.
 
M

Mark A. Odell

Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

I guess so.
Q2) What on earth is the difference between newline and carriage
return?

First, unlike you wrote it in the subject line, they are: \n and \r. A new
line moves the "cursor" down one line, that is, to a new line. A carriage
return brings the "cursor" back to the left margin. Whether they are
treated separately or do the same thing may be implementation specific.
 
P

Pedro Graca

Merrill said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Probably not.
Q2) What on earth is the difference between newline and carriage return?

I think carriage return ('\r') returns to the left margin without
advancing a line;
and newline ('\n') advances a line *and* returns to the left margin



#include <stdio.h>

int main(void) {
printf("1. VERY LONG LINE\r----\n");
printf("2. VERY LONG LINE\n----\n");
printf("3. VERY LONG LINE\r\n----\n");
return 0;
}


Output on Linux (gcc 3.3.5) *and* Windows 2000 (gcc 3.3.3 / DJGPP)
----ERY LONG LINE
2. VERY LONG LINE
 
E

Erik Trulsson

Merrill & Michele said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Since these escape sequences are defined by the ISO C standard, yes.
Q2) What on earth is the difference between newline and carriage return?

Newline ('\n') moves the cursor (or equivalent) to the beginning of the
*next* line, while carriage return ('\r') moves the cursor to the
beginning of the *current* line.


Exactly which character-codes they are represented by is
implementation-defined.

On systems using ASCII (or some derivative thereof) it is usually the
case the \r corresponds to Carriage Return (CR, ASCII code 13) while \n
corresponds to Linefeed (LF, ASCII code 10).

(On some systems (e.g. MS-DOS) it is the case that when you write a \n
to a text file, \n is translated the two characters CR,LF with
corresponding translation in the other direction is done when reading
from a text file. No such translation is done for files opened in
binary mode.)
 
J

Joona I Palaste

I guess so.
First, unlike you wrote it in the subject line, they are: \n and \r. A new
line moves the "cursor" down one line, that is, to a new line. A carriage
return brings the "cursor" back to the left margin. Whether they are
treated separately or do the same thing may be implementation specific.

Seeing as it's now become customary to use \ in all contents where a
divisor sign or an abbreviation for "or" is used, it's only logical that
/ is used as an escape sequence marker in program source code. Bill
doesn't only control our computers, he controls our minds as well.
 
J

john blackburn

Merrill said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Q2) What on earth is the difference between newline and carriage return?

MPJ


An interesting aside: do you know why they always have carriage return
followed by line feed rather than the other way round ?

It goes back to mechanical teletypes; if you issued a line feed followed by
carriage return you got a few characters printed backwards in a diagonal
stripe because there was insufficient time for the carriage to move back to
the left before it started printing. To overcome this, the carriage return
is issued first to give the carriage enough time to return, whilst line
feeding, before printing started. It has stuck as a standard ever since.

:)
 
P

pete

Merrill said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk?

Q2) What on earth is the difference
between newline and carriage return?

int difference = '\n' - '\r';

But serioulsy,
the newline character has a special meaning in terms of text streams.

7.19.2 Streams
[#2] A text stream is an ordered sequence of characters
composed into lines, each line consisting of zero or more
characters plus a terminating new-line character. Whether
the last line requires a terminating new-line character is
implementation-defined.
 
K

Kurt Watzka

john said:
'\r' is simply a carriage return whereas '\n' is a carriage return
followed
by line feed.

Well, from the persprective of this newsgroup, this is certainly wrong.
'\r' is a carriagge return, '\n' is a newline character. From a C
programmers point of view, '\n' is _the_ line separator.
Note that a carriage return is simply that, it moves the
cursor to the beginning of the current line and does not move on to the
next line.

It is here you have to be careful as UNIX text files are of a different
format from Windows text files and need conversion when moving from one
o/s to the other.

On a T200 teletype you had to be carefull to always use _two_ carriage
returns and a linefeed at 75 baud, because the time to move the carriage
from the leftmost position to the rightmost position took almost 1/25
of a second. With '\r\n' you would end up with the first character of
the new line in the middle of the line. However, this is about as relevant
to text file formats from a C programming point of view as the different
_physical_ representations of line separators used in different
environments.

Kurt Watzka
 
H

Herbert Rosenau

'\r' is simply a carriage return whereas '\n' is a carriage return followed
by line feed. Note that a carriage return is simply that, it moves the
cursor to the beginning of the current line and does not move on to the
next line.

No, you should not look on a spezific implementation for that:
\n newline. On one implementation it may be 0x0a,
on another it may be 0x0d, on a third it may be 0x0d0a
on another it may be something else
\r return. on some implementations it is 0x0a
on others it may be something else
\t tab. Often it is 0x09 but other implementations may
use something else

\b bell
\f formfeed
and some more symbolic constant refering to a specific hardware
function - but having different binary codes in different
implementations.

Always get the meaning, not the binary representation. On Win, DOS and
so on \n is 2 binary characters in file, on the stream different:
binary: two, text: only one. On other implementations its only one but
been different to others.
It is here you have to be careful as UNIX text files are of a different
format from Windows text files and need conversion when moving from one o/s
to the other.

There are clearly more implementations with different meaning of them.
It is the C runtime that converts 0x0d0a to only \n - and reverse in
DOS/Win text streams. It is the C runtime that converts \n to 0x0a and
reverse on MAC, it is the C runtime that converts another char(s)
from/to \n.

So whenever you has to handle a text stream use the codes (e.g. \n für
newline). Be sure that you can fall into hard compatibility problems
when the stream does NOT ordinate from your system or is destinated to
another. Yeah, there are filters around that will convert text files
from one system to another and back. Use them to im/export files from
other systems.

When you use a network like tcp/ip the network will do that under
cover for you - except you have to write something on low level.
 
H

Herbert Rosenau

Seeing as it's now become customary to use \ in all contents where a
divisor sign or an abbreviation for "or" is used, it's only logical that
/ is used as an escape sequence marker in program source code. Bill
doesn't only control our computers, he controls our minds as well.

No. '\' is the ecape character in C in any case.
In file pathes you should use '/' as path separator. Even old DOS
understunds it in its APIs even as command.com would not. Whenever you
have to use '\' as path separator you must escape the escape character
('\\' to get it as single character in the source file. Coming in/from
stream the '\' itself is interpreded as path separator.

Has nothing to do with software from Redmont.

Using always '/' as path separator in source makes things much easier.
 
J

Joona I Palaste

No. '\' is the ecape character in C in any case.

Would you believe I knew that?
In file pathes you should use '/' as path separator. Even old DOS
understunds it in its APIs even as command.com would not.

Would you believe I knew that?
Whenever you
have to use '\' as path separator you must escape the escape character
('\\' to get it as single character in the source file. Coming in/from
stream the '\' itself is interpreded as path separator.

Would you believe I knew that?
Has nothing to do with software from Redmont.

Yes it has. 99.9999999999999999999999999999999999999999999999999999999%
of computer users have never seen anything other than '\' as a path
separator. Therefore they think it is, and has always been, the only
path separator in existence. As they use the Bill software more and
more, they become accustomed to it, and it starts replacing '/' in
their minds.
I repeat, IN THEIR MINDS. Not in the actual world. But they start
thinking '\' is the normal slash and '/' is a bastardised abomination,
instead of the other way around.
Using always '/' as path separator in source makes things much easier.

I agree completely, utterly, fully and totally. But Bill, in his
"infinite wisdom", decided otherwise. Microsoft - "if it's a standard,
we'll break it".

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
T

Thomas Matthews

Merrill said:
Section 2.3 of K&R enumerates the complete set of escape sequences.

Q1) Is it possible to ask the following question without getting into
implementation/platform talk? Yes.


Q2) What on earth is the difference between newline and carriage return?
There are three terms involved here: newline, linefeed and carriage
return. They are technically different.

A newline creates a new line, which may involve returning the carriage
to the starting position and advancing one line.

A line feed advances the medium one line text line, which may or may
not cause the carriage to return to the starting position.

A carriage return causes the carriage to return to the start position.
Generally, this does not involve advancing to the next line, but some
output devices may also advance the media after returning the
carriage.

Some newer output devices may also require a Formfeed before the
media is ejected so you can see it.

The exact behavior is implementation defined and not portable. There
are many output devices that contradict the classical definitions.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Joona I Palaste wrote:
[snip]
But they start
thinking '\' is the normal slash and '/' is a bastardised abomination,
instead of the other way around.

Sad, but truer than you realize.

A year or so ago, I heard a radio announcer read off a URL as "aich tea
tea pea colon BACKSLASH BACKSLASH double-u double-u double-u ..."

Apparently, he thought that the \ was called "slash", and the / was
called "backslash".


- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBdp2ragVFX4UWr64RAkxuAKD2PjnlYKv+ow2/CLCF2UkrZrHigwCg1Qbp
B+w9PAvlpEfy2R0rqUlsF3g=
=aWcl
-----END PGP SIGNATURE-----
 
M

Mark A. Odell

Using always '/' as path separator in source makes things much easier.

I do not believe compilers are required to support '/' as a path separator
as I do not believe ISO C specifies such a thing.
 
J

john blackburn

Joona said:
I agree completely, utterly, fully and totally. But Bill, in his
"infinite wisdom", decided otherwise. Microsoft - "if it's a standard,
we'll break it".

I always like to think that the '/' character in web URL's and the like are
a nice reminder that the internet is a UNIX invention.

Bill does have a point in the standards arena though; it's not what is
technically best nor what is most technically appropriate nor indeed what
has set the precedent that matters; it is politics and money that matters
(with the emphasis on the latter). This has been proven time and again with
the classic example being VHS versus Betamax.
 
K

Keith Thompson

Lew Pitcher said:
Joona I Palaste wrote:
[snip]
But they start
thinking '\' is the normal slash and '/' is a bastardised abomination,
instead of the other way around.

Sad, but truer than you realize.

A year or so ago, I heard a radio announcer read off a URL as "aich tea
tea pea colon BACKSLASH BACKSLASH double-u double-u double-u ..."

Apparently, he thought that the \ was called "slash", and the / was
called "backslash".

Either that, or he uses a browser that allows "http:\\www.whatever"
(though the browser in question does display URLs with the proper '/'
characters).
 
K

Keith Thompson

Herbert Rosenau said:
No. '\' is the ecape character in C in any case.
In file pathes you should use '/' as path separator. Even old DOS
understunds it in its APIs even as command.com would not. Whenever you
have to use '\' as path separator you must escape the escape character
('\\' to get it as single character in the source file. Coming in/from
stream the '\' itself is interpreded as path separator.

Has nothing to do with software from Redmont.

Using always '/' as path separator in source makes things much easier.

The choice of which character is used as a path separator depends on
the operating system. ISO C has no concept of path separators, or
even directories. File names are strings; the interpretation of that
string is entirely system-specific.

<OT>
Unix-like systems use '/' as a path separator. MS-DOS and Windows
normally use '\', but allows it to be replaced with '/' in many (but
not all) contexts. VMS uses '.', but as part of a more complex
syntax. I think some systems have used '>' and ':'.

(Yes, I know that the C character constant for backslash is '\\'; I'm
writing English, not C.)

It's commonly suggested that programmers should use '/' as a path
separator on Windows. I suggest that comp.os.ms-windows.programmer.win32
would be a good place to discuss the pros and cons.
</OT>
 
M

Mark McIntyre

Q2) What on earth is the difference between newline and carriage return?

Think about a typewriter. Carriage return moves the carriage back to the
starting point. Newline moves it to a new line.
 
M

Mark McIntyre

In file pathes you should use '/' as path separator. Even old DOS
understunds it in its APIs even as command.com would not.

There was a hack to fix this. I would explain it, but there's not enough
room in this margin.
 

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

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top