Difference between \n and \r?
Difference between \n and \r?
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?
Answer by YOU for Difference between \n and \r?
\r
is char code 13 and \n
is char code 10
windows use \r\n
, linux use \n
and mac use \r
as far as I know.
Answer by pavium for Difference between \n and \r?
Two different characters.
\n
is used as an end-of-line terminator in Unix text files
\r
is used as an end-of-line terminator in Mac text files
\r\n
(ie both) are used to terminate lines in Windows and DOS text files.
Answer by tster for Difference between \n and \r?
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.
Answer by a432511 for Difference between \n and \r?
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.
Answer by Alex Martelli for Difference between \n and \r?
In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).
But seriously, there are many:
- in Unix and all Unix-like systems,
\n
is the code for end-of-line,\r
means nothing special - as a consequence, in C and most languages that somehow copy it (even remotely),
\n
is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed) - in old Mac systems (pre-OS X),
\r
was the code for end-of-line instead - in Windows (and many old OSs), the code for end of line is 2 characters,
\r\n
, in this order - as a (surprising;-) consequence (harking back to OSs much older than Windows),
\r\n
is the standard line-termination for text formats on the Internet - for electromechanical teletype-like "terminals",
\r
commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation),\n
commands the roller to roll up one line (a much faster operation) -- that's the reason you always have\r
before\n
, so that the roller can move while the carriage is still going leftwards!-) - for character-mode terminals (typically emulating even-older printing ones as above), in raw mode,
\r
and\n
act similarly (except both in terms of the cursor, as there is no carriage or roller;-)
In practice, in the modern context of writing to a text file, you should always use \n
(the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r
is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).
Answer by karthik gorijavolu for Difference between \n and \r?
#include 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?
Answer by Akarun for Difference between \n and \r?
To complete,
In a shell (bash) script, you can use \r
to send cursor, in front on line and, of course \n
to put cursor on a new line.
For example, try :
echo -en "AA--AA" ; echo -en "BB" ; echo -en "\rBB"
- The first "echo" display
AA--AA
- The second :
AA--AABB
- The last :
BB--AABB
But don't forget to use -en
as parameters.
Answer by Neil Roy for Difference between \n and \r?
In windows, the \n moves to the beginning of the next line. The \r moves to the beginning of the current line, without moving to the next line. I have used \r in my own console apps where I am testing out some code and I don't want to see text scrolling up my screen, so rather than use \n after printing out some text, of say, a frame rate (FPS), I will printf("%-10d\r", fps); This will return the cursor to the beginning of the line without moving down to the next line and allow me to have other information on the screen that doesn't get scrolled off while the framerate constantly updates on the same line (the %-10 makes certain the output is at least 10 characters, left justified so it ends up padded by spaces, overwriting any old values for that line). It's quite handy for stuff like this, usually when I have debugging stuff output to my console screen.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment