Carriage Return (CR): control character that resets the line position
Carriage return (CR) is a control character that returns the print head or cursor to the start of a line. It appears in text encodings, legacy devices, and modern computing line-ending conventions.
Overview
The carriage return, commonly abbreviated CR and represented in many programming contexts as '\r', is a control character that moves a printing mechanism or cursor back to the beginning of the current line. In character encodings it occupies a defined code point and is often used together with other control characters to mark line breaks; see the related concept of newline.
Image gallery
1 ImageCharacteristics
In the ASCII character set the carriage return is assigned decimal value 13; in Unicode it appears at U+000D. Its traditional physical role comes from typewriters and printers, where a carriage (holding the paper) had to be returned to the left margin before the next line could be typed. In computing the CR character performs the logical equivalent: reposition the cursor to column zero without advancing to the next line by itself.
History and origin
The name and action derive from mechanical devices: a carriage on a typewriter or the print head on early printers had to be moved back to start a new line. Teletypes and teleprinters implemented an electrical or mechanical signal for this action. As electronic text processing developed, the control character was retained in character sets and communication protocols to preserve compatibility with older equipment and conventions.
Uses and examples
Different systems combine CR with other characters to mark line endings. Common conventions include:
- Windows and many network protocols use a pair: CR+LF (carriage return followed by line feed).
- Unix and Unix-like systems typically use LF (line feed) alone to end lines.
- Classic Mac OS historically used CR alone; modern macOS uses LF like other Unix systems.
Programmers often encounter '\r' when parsing text files, handling network protocols, or writing terminal output. A practical trick is to emit a carriage return without a following line feed to return the cursor and overwrite the current line — commonly used for updating progress indicators in command-line tools.
Notable distinctions
Although CR and LF are both control characters related to line handling, they are distinct: CR resets horizontal position while LF advances vertically. Their combined or separate use has led to interoperability issues across platforms, requiring awareness when transferring text files or communicating between systems. Understanding CR's origin and behavior helps explain legacy formats and current practices in text processing.
Related articles
Author
AlegsaOnline.com Carriage Return (CR): control character that resets the line position Leandro Alegsa
URL: https://en.alegsaonline.com/art/17244