Javatpoint Logo
Javatpoint Logo

What is the use of "\r" in C?

The "\r" character in C programming language is known as "Carriage Return", which helps in moving the cursor to the beginning of the current line without advancing or jumping to the next line.

In a few cases, you might want to add additional text without introducing a new line to the console or terminal window's current line. In certain circumstances, you can write new text on top of the existing content by using the "\r" character to bring the cursor back to the beginning of the line.

Example code snippet:

Output:

Loading: 0
Loading: 1
Loading: 2
Loading: 3
Loading: 4
Loading: 5
Loading: 6
Loading: 7
Loading: 8
Loading: 9

Explanation:

This program counts from 0 to 9, but instead of producing new lines after each iteration of the loop, it just overwrites the previous output. In order for the subsequent iteration of the loop to begin replacing the previous output from the left side of the console window, the "\r" character brings the cursor back to the beginning of the line.

Note: The output buffer is flushed using the fflush(stdout) call, causing the output to be immediately printed to the console. The goal of using "\r" to update the output in real time would be defeated without this call if the output was postponed until the end of the program.

There is some other additional information about "\r" in C programming which are as follows:

  • A single character in C is represented by the character literal "\r". It has the same ASCII code as the carriage return character in other computer languages, which is 13, making it easy to identify.
  • Complex output formatting can be created by combining the "\r" character with other control characters like "\n" (newline) and "\t" (tab).
  • To ensure that the output is quickly provided to the console after updating the output on the console or terminal window with "\r", it's essential to flush the output buffer with fflush(stdout). If not, the previous output could be cached in the output buffer and delayed from being displayed.
  • The "\r" command can occasionally be used to output animated or dynamic text on the console. For instance, you could use "\r" to build a progress bar that updates while a lengthy task is finished.
  • When creating custom console output in C, "\r" is frequently used in conjunction with other console output functions like puts() and printf() to control the output formatting.
  • In command-line interfaces (CLIs) and other text-based programs, the update symbol "\r" is frequently used to show the status of an action, such as downloading a file, transferring data, or compiling code.
  • The "\r" is particularly helpful for updating a single line of output without scrolling the entire terminal window. Working with large datasets or lengthy procedures can benefit from this.
  • Additional special characters in C, such as "\b" (backspace), "\v" (vertical tab), and "\r" (return), can be used to modify the output formatting in addition to "\r". These additional special characters shift the cursor back one character and down one line, respectively.
  • In addition to being used in C programming, "\r" can also be used to control console output in Java, Python, and Ruby.
  • Making ensuring that the new output is the same length as the previous output or greater is crucial when using "\r" to refresh the output on the console. Characters from the previous output that were not overwritten if the new output is shorter than the old output may cause output to be distorted or inaccurate.
  • The"\r" does not clear the line or remove any text; it just advances the cursor to the start of the current line. If you want to format the line before writing new output, use "\r" and other control characters, like spaces or backspaces, to replace the old text with blank spaces.
  • The "\r" can be used to modify both input and output formatting in conjunction with other terminal output functions like scanf() and gets(). For instance, use "\r" to make a command-line prompt that changes as the user types input.

Example:

Another code snippet demonstrating the use of "\r" in C to create an animated loading spinner:

Output:

Loading \
Loading -
Loading +
Loading {
Loading \
Loading -
Loading +
Loading {
Loading \
Loading -
Done!!!!!!






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA