C Programming

What is \r in C Language

C is a general-purpose programming language widely used in operating systems and other real time applications. It also provides low-level access to memory and language constructs, allowing it to map effectively to machine instructions. While writing a code in C language, you may find different characters that are widely used in your code and without them, it’s hard to write an effective C program. Among these characters, “\r” is a widely used one.

If you’re unaware of it, use the instructions in this page to learn everything there is to know about the C language’s “r” symbol.

What does \r Mean in the C Language

In C programming, a newline or line break (commonly also referred to as a linefeed or LF) is a special character or sequence of characters that signals the end of a line of text. The carriage return character is represented by the letter “r” in ASCII. This character is used to move the cursor or pointer to the beginning of the current line, regardless of where it is on the page. It is often used for line breaks when entering text into a text editor or terminal. The “\r” character was originally coined CRLF, standing for carriage return line feed, but has since become shortened to simply “\r”.

To help you grasp the function of r, let’s start with a few examples.

Example 1

Here is a simple code with the “\r” character inside a C code.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello! This is\r";
    cout << "An article on the carriage return";
    return 0;
}

In the above code, we are using two print statements, but after one of them we are using “\r”. This will move the cursor to the beginning of the current line and only that line will be printed.

Output

Example 2

The most common way to represent the letter “r” is as a carriage return followed by a line feed (rn). The combination of these characters is also referred to as a “carriage return” or “newline” and is often symbolic of the end of a particular command in C programming language. This is important when the user needs to make changes or edit a previously entered line of text.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello! \r\n This is";
    cout << "An article on the carriage return";
    return 0;
}

Now in this code, \r\n is used in the middle of the first print statement, so only ‘Hello!’ is printed on one line and the rest of the code is shifted to the new line.

Output

In addition, the “\r” character is frequently used in combination with other characters in C programming language for various functions such as setting up a loop, entering conditional statements, and displaying text on the screen. For example, the printf command is used to display output to the screen, and it often requires a “\r” in order to move the cursor to the beginning of the line before displaying the output.

Furthermore, the “\r” character is also used for formatting text by turning off the print flag for the carriage return character. This allows for text to be written without the need for a new line at the end of each line. This is useful in situations such as when displaying text in a specific format on the screen.

Note: \r and \n are similar in nature, but the only distinction is that the “r” character returns the cursor to the start of the current line, whereas the “n” character transfers it to the next line.

Conclusion

In conclusion, the “\r” character is a key element of the C programming language. It’s employed to move the pointer back to the start of the current line. It is used in combination with other characters to accomplish various tasks, and it can also be used in formatting text. The user has better control over the results of their code thanks to all these features and more.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.