Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi friends. i want to know what does mean "\r"("Carriage return") and how to works it
Posted
Updated 10-Oct-22 14:41pm

By historical reasons, there are two old characters used to denote the boundary between lines (or paragraphs, depending in interpretation) for text files. One is "carriage return", "\r", and another one is "line feed", "\n" (also: "new line"). You can find them in the ASCII table: ASCII — Wikipedia, the free encyclopedia[^].

It's funny, but these characters mimic behavior of mechanical typewriter. The letters hits the paper pressed against the rubberized bar; the bar with the paper is mounted in the "carriage" (it "carries" it) which moves horizontally to move from one character position to the next one. When the line is complete, it moves all the way to the left. It corresponds to "\r", "carriage return". Then the bar rotates by one line, and it corresponds to "\n". These two operations are kept independent, because some typing tricks requires moving just the carriage to type over already typed text, and independent rotation of the bar, to print something on an arbitrary line. So these operations sneaked in computer notation, even though no printer actually used them the way the mechanical typewriter did.

In Unicode, there are different characters used for similar purposes, but "\r" and "\n" are used more often.

Worse, the rules for using these characters depend on the platform. Please see: Newline — Wikipedia, the free encyclopedia[^].

In .NET programs, in most cases (in fact, it also depends on purpose), you need to use platform-dependent object instead, System.Environment.NewLine:
Environment Class (System)[^].

Still, if you copy one text file from one platform to another, on a target platform, you can get "non-native" use of new line characters. These days, this is not a big problem. The role of text files is reduced to the media for the file format where it does not matter, such as, say, HTML, ePUB, Open XML, and so on. On all platform, modern text handling software, including text editors, are designed to detect mismatch and fix it, "normalize".

—SA
 
Share this answer
 
v4
Comments
CPallini 21-Jan-16 2:50am    
5.
Sergey Alexandrovich Kryukov 21-Jan-16 2:52am    
Thank you, Carlo.
By the way, I forgot to add a paragraph (just did it) where I explained the origination of "\n" and "\r" from the design of mechanical typewriter. This is pretty funny, take a look. (I had a questionable "pleasure" of typing a bit using some of such machines, which is not really fun. :-)
—SA
Basically A carriage return \r moves the cursor to the beginning of the current line. A newline \n causes a drop to the next line and possibly the beginning of the next line; That's the platform dependent part that Alexei notes above (on a *nix system \n gives you both a carriage return and a newline, in windows it doesn't)
 
Share this answer
 
Beware, "\r\n" works and "\n\r" does not work.
Always include the quotation marks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900