the difference between \r and \n

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • YarvinG Liu

    the difference between \r and \n

    i write two codes:
    printf("123\r") ;
    printf("123\n") ;
    the lattar. as we all know. the output will be:
    123
    press any key to continue

    but the formmer will be:
    press any key to continue

    which means data:123 has lost.
    Y?
  • Richard Heathfield

    #2
    Re: the difference between \r and \n

    YarvinG Liu said:
    i write two codes:
    printf("123\r") ;
    printf("123\n") ;
    the lattar. as we all know. the output will be:
    123
    press any key to continue
    >
    but the formmer will be:
    press any key to continue
    >
    which means data:123 has lost.
    Right.
    Y?
    I don't understand. If you mean "Why?", however, the answer is that writing
    a '\r' character to the standard output stream moves the active position
    to the initial position of the current line. It would seem that your IDE
    then writes "press any key to continue" to the standard output stream,
    starting at that position, and therefore overwriting your "123" text.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Dan

      #3
      Re: the difference between \r and \n


      "YarvinG Liu" <yarving@gmail. comwrote in message
      news:be73985c-a476-44a2-b1b0-51d01458e6b0@h1 g2000prh.google groups.com...
      >i write two codes:
      printf("123\r") ;
      printf("123\n") ;
      the lattar. as we all know. the output will be:
      123
      press any key to continue
      >
      but the formmer will be:
      press any key to continue
      >
      which means data:123 has lost.
      Y?
      \r move the cursor back to the start of the line, the "press any key.." is
      then written over the top of 123. Its probably safest to always go \r\n if
      you are on windows systems.


      Comment

      • Richard Heathfield

        #4
        Re: the difference between \r and \n

        Dan said:

        <snip>
        \r move the cursor back to the start of the line, the "press any key.."
        is then written over the top of 123. Its probably safest to always go
        \r\n if you are on windows systems.
        No, that's pointless - \n is perfectly adequate on *all* systems, including
        Windows. The stdout stream is a text stream. Like all text streams, if you
        write a '\n' to it, any necessary conversions *will* be done by the
        implementation.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        Working...