The difference between and \r in software code

Follow+Starred PublicNo., don’t miss exciting content

6871378dd34a86985a7d7c6330499804.gif

Orchestration | strongerHuang

WeChat public account | Embedded column

When we use printf to print, we basically use control characters such as \n and \r, such as:

printf("hello world!\r\n");

So do you know the difference between \n and \r?

About \n and \r

In ASCII code, we will see a type of non-displayable characters called control characters, which include control characters such as \r and \n.

9382428896a1f7384b69ce9b0293d1b4.png

\n : Line break character (newline), starting a new line, corresponding to ASCII value 10 (abbreviation: LF).

\r :  Carriage return character (return), returns to the beginning of a line, corresponding to ASCII value 13 (abbreviation: CR).

Carriage return and line feed sources:

Before computers existed, there was a thing called a Teletype Model 33 that could type 10 characters per second. But there is a problem with it, that is, it takes 0.2 seconds to type a line and break it, which is enough to type two characters. If a new character is transmitted during these 0.2 seconds, this character will be lost.

Therefore, the developers thought of a way to solve this problem, which was to add two characters to indicate the end of each line. One is called "carriage return", which tells the typewriter to position the print head at the left border; the other is called "line feed", which tells the typewriter to move the paper down one line.

This is the origin of "line feed" and "carriage return", which can be seen from their English names.

Difference between \n and \r

Later, the computer was invented, and these two concepts were moved to the computer. At that time, memory was expensive, and some scientists thought it was too wasteful to add two characters at the end of each line. Just add one. So, there was a disagreement.

'\r' is a carriage return, and '\n' is a line feed. The former moves the cursor to the beginning of the line, and the latter moves the cursor down one space. The commonly used Enter is the sum of two.

Some editors only recognize \r\n, and some editors recognize both. Therefore, if you want to use it universally, it is best to use \r\n to break the line.

  • In Microsoft's MS-DOS and Windows, the two characters "Carriage Return CR ('\r')" and "Line Feed LF ('\n')" are used as line breaks;

  • In Windows system, the end of each line is carriage return + line feed (CR+LF), that is, "\r\n";

  • In Unix systems, there is only a newline CR at the end of each line, that is, "\n";

  • In the Mac system, the end of each line is carriage return CR, that is, '\r';

  • Therefore, the carriage return character we usually use when writing files should be precisely called carriage return and line feed;

Influence

A direct consequence is that if a file under Unix/Mac system is opened in Windows, all text will become one line; and if a file in Windows is opened under Unix/Mac, there may be an extra ^M at the end of each line. symbol.

When files saved in Linux are viewed with Notepad on Windows, black spots will appear. Many people should have seen this. For example, the newline character is displayed directly in the Keil code:

25abf7b9710f6be18bd06b5881d0d39e.png

Convert each other

Under Linux, the command unix2dos is to convert the linux file format into the windows file format, and the command dos2unix is ​​to convert the windows format into the linux file format.

When using FTP software to transfer files between different platforms, some FTP client programs will automatically convert the newline format in the ascii text mode transfer mode. The number of bytes in the file after this transfer may change.

If you don't want ftp to modify the original file, you can use bin mode (binary mode) to transfer text. When a program is run on Windows, it generates a text file in CR/LF format, and when it is run on Linux, it generates a text file in LF format.

------------ END ------------

49fb24bec2bff1083b50f2c27b66b50d.gif

●Column "Embedded Tools"

●Column "Embedded Development"

●Column "Keil Tutorial"

●Embedded column selected tutorials

Follow the public account and reply "Join the group" Join the technical exchange group according to the rules and reply "1024 " See more.

9f1f95449cc3d3454f3e03355b03bd9f.jpeg

6709285c30a21f232b2e0cff51cb5c37.png

Click "Read the original text" to view more shares.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/134844154