\r operator

J

John T.

What is the \r operator? What does it do?

When I write, for example, print("Hi there\n"); I know the \n stands for
newline, but what are the meaning of the command:

print("Hi There\n\r"); ?
 
S

Shill

What is the \r operator? What does it do?
When I write, for example, print("Hi there\n"); I know the \n stands for
newline, but what are the meaning of the command:

print("Hi There\n\r"); ?

'\r' is not an operator, merely a control character.

'\r' is CARRIAGE RETURN (encoded 0xD in ASCII)
'\n' is LINE FEED (encoded 0xA in ASCII)

e.g. HTTP expects "\r\n" as the end-of-line marker.
 
E

Emmanuel Delahaye

John T. said:
What is the \r operator? What does it do?

It's not an operator, it's a (special) character.
When I write, for example, print("Hi there\n"); I know the \n stands for
newline, but what are the meaning of the command:

print("Hi There\n\r"); ?

<n869>

WG14/N869 Committee Draft — January 18, 1999 21

5.2.2 Character display semantics
<...>
2 Alphabetic escape sequences representing nongraphic characters in the
execution character set are intended to produce actions on display devices
as follows:
<...>
\r (carriage return) Moves the active position to the initial
position of the current line.

</>
 
S

Simon Biber

Artemio said:
\r stands for "carriage return".

And has the meaning to move the cursor back to the beginning of
the current line so you can overwrite any text already printed.
Not all output devices actually support these semantics.
On window$ machines, in text files, they have "\n\r" at the end
of each line. In unix, lines end by "\n" only.

I think you mean "\r\n" not "\n\r". MS-DOS text files have CRLF
between each line, that would be "\r\n" but only if you wrongly
read the file in binary mode. In text mode any platform's native
text file format is internally converted to and from Unix form
with just a '\n', so you can write portable code that can work
with text files on any platform without having to know what
format it uses to store them.
That's why unix text files in windows look like one huge line.

Unless you use a good text editor that auto-detects the line
endings and lets you quickly convert between file types.
 
R

Richard Heathfield

Artemio wrote:

On window$ machines, in text files, they have "\n\r" at the end of each
line.

It's actually \r\n, rather than \n\r. Note that the C runtime library sorts
this all out for you (for text files), so you can pretend you only have to
deal with \n.

<snip>
 
A

Artemio

But other people /do/ care, and misleading information can confuse them.
If you don't ***know*** the answer for ***sure***, it's best to stay
quiet. This is a lesson I learned the hard way. :)

Thanks, I got it.

Good luck!
 
K

Keith Thompson

Richard Heathfield said:
Artemio wrote:



It's actually \r\n, rather than \n\r. Note that the C runtime library sorts
this all out for you (for text files), so you can pretend you only have to
deal with \n.

Note also that '\n' represents the ASCII LF character (numeric value
10) on many systems, but the standard doesn't require that particular
representation. On a system that uses the ASCII CR character as a
line terminator in text files, a C implementation could sensibly
represent '\n' as CR (numeric value 13). Or it could represent it as
LF for consistency with Unix implementations, and map it to CR on text
input and output. (Giving '\n' and '\r' the same representation could
cause problems, but I'm not going to think about that.)

On non-ASCII systems, of course, all bets are off.
 
A

Artemio

John said:
What is the \r operator? What does it do?

When I write, for example, print("Hi there\n"); I know the \n stands for
newline, but what are the meaning of the command:

print("Hi There\n\r"); ?

\r stands for "carriage return".

On window$ machines, in text files, they have "\n\r" at the end of each
line. In unix, lines end by "\n" only.

That's why unix text files in windows look like one huge line.
 
A

Artemio

Hey guys I actually didn't want to start any debates here.

I don't work with window$ etc. so I don't care whether it's \n\r or \r\n

:)
 
D

Dan Pop

Here's a typical usage of \r. Error checking on time() calls deliberately
omitted.

#include <stdio.h>
#include <time.h>

void waste1sec(void)
{
time_t t = time(NULL);
while ((int)(difftime(time(NULL), t)) == 0) ;
}

int main()
{
int secs;
for (secs = 10; secs >= 0; secs--) {
waste1sec();
printf("%2d\r", secs);
fflush(stdout);
}
return 0;
}

Notes:

1. There are better ways of doing nothing for one second (e.g. sleep() or
Sleep()), but none of them is portable. Don't use my method in real
world applications designed for multitasking systems.

2. The program is designed for CRT-based terminals; its output is going
to be somewhat different on printer-based terminals.

Dan
 
D

Dave Thompson

On 29 Jun 2003 03:13:14 -0700 said:
fputs("I'll be right\rback!\n");

Then on a typical computer console you should see:

back!be right
Or rather on nearly all video displays, including also (remote)
terminals if you can still find one in use.
This is because "\r" is defined to mean to return back to the
beginning of the current line. On an old teletype machine, you'd see
"I'll be right" with the "I'll " being over-striked by the letters in
"back!".
Actually the most common old Teletypes (models 33 and 35) didn't
have lowercase and I don't remember if they even escaped the carriage
for 0x60-0x7E. But you would get overstriking on other printing
terminals/devices, including DECwriter, (GE) Terminet, etc., and also
on storage-tube video (Tektronix).

(BTW that tense, I think it's a participle, is 'overstruck'.)

- David.Thompson1 at worldnet.att.net
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top