\r operator

Discussion in 'C Programming' started by John T., Jun 28, 2003.

  1. John T.

    John T. Guest

    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"); ?
     
    John T., Jun 28, 2003
    #1
    1. Advertising

  2. John T.

    Shill Guest

    > 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.
     
    Shill, Jun 28, 2003
    #2
    1. Advertising

  3. In 'comp.lang.c', "John T." <> wrote:

    > 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.

    </>

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-library: http://www.dinkumware.com/htm_cl/index.html
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jun 28, 2003
    #3
  4. John T.

    Simon Biber Guest

    "Artemio" <> wrote:
    > \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.

    --
    Simon.
     
    Simon Biber, Jun 28, 2003
    #4
  5. Artemio wrote:

    <snip>
    >
    > 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>

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Jun 28, 2003
    #5
  6. John T.

    Artemio Guest

    > 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!
     
    Artemio, Jun 28, 2003
    #6
  7. Richard Heathfield <> writes:
    > Artemio wrote:
    >
    > <snip>
    > >
    > > 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.


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Jun 28, 2003
    #7
  8. John T.

    Artemio Guest

    John T. wrote:

    > 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.
     
    Artemio, Jun 29, 2003
    #8
  9. John T.

    Artemio Guest

    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

    :)
     
    Artemio, Jun 29, 2003
    #9
  10. John T.

    Dan Pop Guest

    In <bdjord$4lu$-c.dk> "John T." <> writes:

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


    What does your C book say about it?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 30, 2003
    #10
  11. John T.

    Dan Pop Guest

    In <bdjrlv$1tq0$> Shill <> writes:

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


    This is common to most ASCII text-based network protocols.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 30, 2003
    #11
  12. John T.

    Dan Pop Guest

    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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 30, 2003
    #12
  13. On 29 Jun 2003 03:13:14 -0700, Micah Cowan <> wrote:
    [snip]
    > 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
     
    Dave Thompson, Jul 4, 2003
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    587
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,053
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,030
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,689
    Kanenas
    May 30, 2005
Loading...

Share This Page