Difference between \n and \r?

前端 未结 10 695
予麋鹿
予麋鹿 2020-11-21 11:17

What’s the difference between \\n (newline) and \\r (carriage return)?

In particular, are there any practical differences between

相关标签:
10条回答
  • 2020-11-21 11:50

    What’s the difference between \n (newline) and \r (carriage return)?

    In particular, are there any practical differences between \n and \r? Are there places where one should be used instead of the other?


    I would like to make a short experiment with the respective escape sequences of \n for newline and \r for carriage return to illustrate where the distinct difference between them is.

    I know, that this question was asked as language-independent. Nonetheless, We need a language at least in order to fulfill the experiment. In my case, I`ve chosen C++, but the experiment shall generally be applicable in any programming language.

    The program simply just iterates to print a sentence into the console, done by a for-loop iteration.


    Newline program:

    #include <iostream>
    
    int main(void)
    {
        for(int i = 0; i < 7; i++)
        {
           std::cout << i + 1  <<".Walkthrough of the for-loop \n";   // Notice `\n` at the end.
        }
        return 0;
    }
    

    Output:

    1.Walkthrough of the for-loop
    2.Walkthrough of the for-loop
    3.Walkthrough of the for-loop
    4.Walkthrough of the for-loop
    5.Walkthrough of the for-loop
    6.Walkthrough of the for-loop
    7.Walkthrough of the for-loop
    

    Notice, that this result will not be provided on any system, you are executing this C++ code. But it shall work for the most modern systems. Read below for more details.


    Now, the same program, but with the difference, that \n is replaced by \r at the end of the print sequence.

    Carriage return program:

    #include <iostream>
    
    int main(void)
    {
        for(int i = 0; i < 7; i++)
        {
           std::cout << i + 1  <<".Walkthrough of the for-loop \r";   // Notice `\r` at the end.
        }
        return 0;
    }
    

    Output:

    7.Walkthrough of the for-loop 
    

    Noticed where the difference is? The difference is simply as that, when you using the Carriage return escape sequence \r at the end of each print sequence, the next iteration of this sequence do not getting into the following text line - At the end of each print sequence, the cursor did not jumped to the *beginning of the next line.

    Instead, the cursor jumped back to the beginning of the line, on which he has been at the end of, before using the \r character. - The result is that each following iteration of the print sequence is replacing the previous one.

    *Note: A \n do not necessarily jump to the beginning of following text line. On some, in general more elder, operation systems the result of the \n newline character can be, that it jumps to anywhere in the following line, not just to the beginning. That is why, they rquire to use \r \n to get at the start of the next text line.


    This experiment showed us the difference between newline and carriage return in the context of the output of the iteration of a print sequence.

    When discussing about the input in a program, some terminals/consoles may convert a carriage return into a newline implicitly for better portability, compatibility and integrity.

    But if you have the choice to choose one for another or want or need to explicitly use only a specific one, you should always operate with the one, which fits to its purpose and strictly distinguish between.

    0 讨论(0)
  • 2020-11-21 11:50
    #include <stdio.h>
    
    void main()
    {
      int countch=0;
      int countwd=1;
    
      printf("Enter your sentence in lowercase: ");
      char ch='a';
      while(ch!='\r')
      {
        ch=getche();
        if(ch==' ')
          countwd++;
        else
          countch++;
      }
    
      printf("\n Words = ",countwd);
    
      printf("Characters = ",countch-1);
    
      getch();
    
    }
    

    lets take this example try putting \n in place of \r it will not work and try to guess why?

    0 讨论(0)
  • 2020-11-21 11:52

    Since nobody else mentioned it specifically (are they too young to know/remember?) - I suspect the use of \r\n originated for typewriters and similar devices.

    When you wanted a new line while using a multi-line-capable typewriter, there were two physical actions it had to perform: slide the carriage back to the beginning (left, in US) of the page, and feed the paper up one notch.

    Back in the days of line printers the only way to do bold text, for example, was to do a carriage return WITHOUT a newline and print the same characters over the old ones, thus adding more ink, thus making it appear darker (bolded). When the mechanical "newline" function failed in a typewriter, this was the annoying result: you could type over the previous line of text if you weren't paying attention.

    0 讨论(0)
  • 2020-11-21 11:58

    Two different characters for different Operating Systems. Also this plays a role in data transmitted over TCP/IP which requires the use of \r\n.

    \n Unix

    \r Mac

    \r\n Windows and DOS.

    0 讨论(0)
  • 2020-11-21 12:00

    Historically a \n was used to move the carriage down, while the \r was used to move the carriage back to the left side of the page.

    0 讨论(0)
  • 2020-11-21 12:00

    Just to add to the confusion, I've been working on a simple text editor using a TextArea element in an HTML page in a browser. In anticipation of compatibility woes with respect to CR/LF, I wrote the code to check the platform, and use whichever newline convention was applicable to the platform.

    However, I discovered something interesting when checking the actual characters contained in the TextArea, via a small JavaScript function that generates the hex data corresponding to the characters.

    For the test, I typed in the following text:

    Hello, World[enter]

    Goodbye, Cruel World[enter]

    When I examined the text data, the byte sequence I obtained was this:

    48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 0a 47 6f 6f 64 62 79 65 2c 20 43 72 75 65 6c 20 57 6f 72 6c 64 0a

    Now, most people looking at this, and seeing 0a but no 0d bytes, would think that this output was obtained on a Unix/Linux platform. But, here's the rub: this sequence I obtained in Google Chrome on Windows 7 64-bit.

    So, if you're using a TextArea element and examining the text, CHECK the output as I've done above, to make sure what actual character bytes are returned from your TextArea. I've yet to see if this differs on other platforms or other browsers, but it's worth bearing in mind if you're performing text processing via JavaScript, and you need to make that text processing platform independent.

    The conventions covered in above posts apply to console output, but HTML elements, it appears, adhere to the UNIX/Linux convention. Unless someone discovers otherwise on a different platform/browser.

    0 讨论(0)
提交回复
热议问题