Difference between \n and \r?

前端 未结 10 773
予麋鹿
予麋鹿 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 
    
    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 
    
    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.

提交回复
热议问题