By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,146 Members | 1,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,146 IT Pros & Developers. It's quick & easy.

What is "\r" represent?

P: n/a
I check the ISO 9899 standard,

\r (carriage return) Movesthe active position to the initial position
of the current line.

What does it do? How to input?

I tried to use Flex to check this pattern as below:

%%
[a-zA-Z]\r printf( "Find a carriage return" );

I tried different input but it never matches.

What is the different of carriage return with the return on keyboard?
The RETURN we usually use is just a newline?
Mar 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <m2************@stu.ust.hk>,
Matthew Zhou <zh*********@gmail.comwrote:
>I check the ISO 9899 standard,
>\r (carriage return) Moves the active position to the initial position
of the current line.
>What does it do? How to input?
>I tried to use Flex to check this pattern as below:
>%%
[a-zA-Z]\r printf( "Find a carriage return" );
>I tried different input but it never matches.
>What is the different of carriage return with the return on keyboard?
The RETURN we usually use is just a newline?
The carriage return character might be used internally by the operating
system (possibly in conjunction with other characters) to indicate the
end of line for a text file. When you read such a file in (with the
same OS) as a text file, these internal line terminations are
automatically translated by the OS to appear to you as \n . If somehow
the text file managed to have a \r that is not part of the OS's line
termination sequence, then exactly what happens is up to the OS: some
of them might translate it to \n and some might leave it as a \r and
some might decide that there is an I/O error on the file.

In most OS's, you can write a stand-alone \r to a text file (one that
is not part of a line termination sequence) by simply coding '\r' as
the output character. You can't count on this coming through on input,
though, for the reasons mentioned above... and also because there are
OS's for which the carriage return character is the complete internal
line termination sequence, so writing a \r on those OS's might have the
same effect as writing \n .

If you were to take one of those text files and read it in as
a binary file, then the automatic translation of line terminators
does *not* take place -- binary files don't have line terminators
after all. So if you open a text file in binary mode, you might
be able to see the \r in input. (This can get to be
important for streams that are network sockets, as there
are many protocols such as SMTP in which the standard network
representation of a "line" includes a \r character that must be
silently gotten rid of.)

>\r (carriage return) Moves the active position to the initial position
of the current line.
>What does it do?
On input, \r is just another character, most often silently ignored
(except as noted above.)

The description you are looking at is the standard behaviour
of carriage return when one is output to a device such as a
terminal or human-viewable character display. The meaning of
carriage-return on input is less settled.
How to input?
Other than by writing one to a file and reading that file,
the method of inputing a carriage return at the keyboard is
OS (and user settings) dependant. Usually all of your
keyboard input is being interpreted by something -- e.g.,
something has to know that when you press left-shift and then
the z key, that you mean you want to input the 'Z' character
and not the two seperate keys <<left-shift>and 'z' . Often,
your input is not immediately sent to the program: instead,
your input usually gets buffered up, so that you can edit it
inline (e.g., backspace and press a new character) and only
the completed edited line gets sent. Such systems sometimes
offer no way of entering a carriage-return without it
meaning "newline"; sometimes there are methods such as
pressing control-M, or pressing \ followed by return,
or pressing control-V followed by return -- such methods are
not specified by C (you just might be able to dig such
a method out of POSIX's definition of the Bourne Shell).

--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Mar 16 '07 #2

P: n/a
Matthew Zhou wrote:
I check the ISO 9899 standard,

\r (carriage return) Movesthe active position to the initial position
of the current line.

What does it do?
It moves the cursor or caret to the beginning of the current line. If
it's already at the beginning, the behaviour is unspecified.
How to input?
It's an output character.

<snip>
What is the different of carriage return with the return on keyboard?
The RETURN we usually use is just a newline?
A newline character is C's representation for the system's native
sequence for moving the active position to the start of the next line.
And yes, the Return, (or Enter), is setup to have this effect.

Mar 16 '07 #3

P: n/a
Matthew Zhou wrote, On 16/03/07 17:44:
I check the ISO 9899 standard,

\r (carriage return) Movesthe active position to the initial position
of the current line.

What does it do? How to input?
What do you not understand about "Moves the active position to the
initial position of the current line?" It seems self explanatory to me.
As to how to input it (I assume you mean from the keyboard) that depends
on your system.
I tried to use Flex to check this pattern as below:

%%
[a-zA-Z]\r printf( "Find a carriage return" );

I tried different input but it never matches.
Flex is off topic here.
What is the different of carriage return with the return on keyboard?
The RETURN we usually use is just a newline?
In C when a stream is opened in text mode whatever is used to indicate
and "end of line" which is what one would expect the return key to
produce, is translated to a newline. On DOS and Windows systems, this is
traditionally a carriage return followed by a new line.
--
Flash Gordon
Mar 16 '07 #4

P: n/a
Matthew Zhou wrote:
I check the ISO 9899 standard,

\r (carriage return) Movesthe active position to the initial position
of the current line.

What does it do?

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

#define SIZE 4

void spin_wait(double secs)
{
clock_t start;
clock_t now;
double et;

if (secs <= 0)
return;
start = clock();
while (et < secs)
{
now = clock();
et = (double)(now-start)/CLOCKS_PER_SEC;
}
}

int main(void)
{
char star[SIZE] = "-\\|/";
int i;

while (1)
{
for(i = 0; i < SIZE; i++)
{
printf("\r%c ", star[i]);
fflush(stdout);
spin_wait(.25);
}
}

return 0;
}

Brian

Mar 16 '07 #5

P: n/a
On 16 Mar 2007 22:43:29 GMT, "Default User" <de***********@yahoo.com>
wrote:
>Matthew Zhou wrote:
>I check the ISO 9899 standard,

\r (carriage return) Movesthe active position to the initial position
of the current line.

What does it do?


Try this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

#define SIZE 4

void spin_wait(double secs)
{
clock_t start;
clock_t now;
double et;
Change that to:

double et = 0.0;
>
if (secs <= 0)
return;
start = clock();
while (et < secs)
{
now = clock();
et = (double)(now-start)/CLOCKS_PER_SEC;
}
}

int main(void)
{
char star[SIZE] = "-\\|/";
int i;

while (1)
I'd change that to:

for ( ; ; )

in order to avoid compiler and PC-lint warnings. YMMV.
{
for(i = 0; i < SIZE; i++)
{
printf("\r%c ", star[i]);
The first time through the loop, it performs a carriage return on a
non-existant line. Is that okay? I'm sure this is okay:

printf("%c\r", star[i]);
fflush(stdout);
spin_wait(.25);
}
}

return 0;
I'd change that to:

return 0; /*lint !e527: Unreachable code at token 'return'*/

YMMV.
}
Nice program (really), notwithstanding an uninitialized variable and
some arguably pedantic issues :)

--
jay
Mar 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.