Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

newline - Difference between and ?

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

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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, is the code for end-of-line, means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, , in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows), is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like "terminals", commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), commands the roller to roll up one line (a much faster operation) -- that's the reason you always have before , so that the roller can move while the carriage is still going leftwards!-) Wikipedia has a more detailed explanation.
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, and 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 (the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use 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;-).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...