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

  2. John T.

    Shill Guest

    What is the \r operator? What does it do?
    '\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
    1. Advertisements

  3. It's not an operator, it's a (special) character.

    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.

    Emmanuel Delahaye, Jun 28, 2003
  4. John T.

    Simon Biber Guest

    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.
    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.
    Unless you use a good text editor that auto-detects the line
    endings and lets you quickly convert between file types.
    Simon Biber, Jun 28, 2003
  5. 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.

    Richard Heathfield, Jun 28, 2003
  6. John T.

    Artemio Guest

    But other people /do/ care, and misleading information can confuse them.
    Thanks, I got it.

    Good luck!
    Artemio, Jun 28, 2003
  7. 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, Jun 28, 2003
  8. John T.

    Artemio Guest

    \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
  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
  10. John T.

    Dan Pop Guest

    What does your C book say about it?

    Dan Pop, Jun 30, 2003
  11. John T.

    Dan Pop Guest

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

    Dan Pop, Jun 30, 2003
  12. John T.

    Dan Pop Guest

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

    #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--) {
    printf("%2d\r", secs);
    return 0;


    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 Pop, Jun 30, 2003
  13. Or rather on nearly all video displays, including also (remote)
    terminals if you can still find one in use.
    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
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.