I'm a beginner in C++. I saw \b and \r in a program source code. I've seen \n and I know its purpose. But why do we use \r and \b in C++? Please clear my doubt.
Thank You.

"\n" for new line
"\b" for a backspace, means if u print it, cursor will print and come back 1 character. For example.... cout<<"hello\bHi"; will print "HellHi". because after printing Hello, compiler found a \b escape sequence. so it came 1 character back (at 'o' of Hello) and stat printing Hi from o of Hello..... so u got HellHi instead of HelloHi.

'\r' is used for carriage return to come 1 line back or in start of line.

HOPE I FIXED. MARK THIS THREAD SOLVED AND DO RATE ME

commented: rep - beggar! -1

I'm a beginner in C++. I saw \b and \r in a program source code. I've seen \n and I know its purpose. But why do we use \r and \b in C++? Please clear my doubt.
Thank You.

Those codes are called backslash codes (or character escape sequences), there's a quite logical reason for why they exist:
What for example when you want to have a string containing a newline or backspace character? Of course there's no way to input such a character directly via your keyboard (you can't for example just press the backspace key on your keyboard while you're inputting your code in your text-editor because the only thing it will do is erase the character which is to the left of your text cursor), so we use those codes to let the compiler know which character we want. \b and \n are not the only backslash codes supported by C++, there are some more of them, which you can view by clicking this link.

Post by jonsca and tux4life have divulged all the information related to this topic.But would like to add some from my side.

Apart from the printable characters (character which can be seen on a output screen) we make use of several other operations when working on any text.
When we work on a text editor,the text editor handles all the operations on its own and keeps us away from implementation details.But in C those operations are included in the character set itself so that a user can make use of them and format his/her output in any way required.

These characters which hold a very special meaning and work instead of functions as in text editors are your "Escape Characters".

Every letter has its identity. Say character 'A' instructs the machine to store value of 65 in 1 byte to symbolise 'A'. In escape characters we tell the compiler to escape or neglect the meaning of original character and act differently by placing the '\' symbol before it.Hence the name "Escape Character". You can try something like:

cout << "TestString1__TestString2";

and place '\a' '\b' '\c' ... and so on and test what every character does.

\b = it's purpose is backspace
\r = purpose is carriage return

i want the purpose of for(;;)
please clear my doubt
thank you

\b = it's purpose is backspace
\r = purpose is carriage return

i want the purpose of for(;;)
please clear my doubt
thank you

for(;;) is an infinite loop and used where the number of iterations is unknown.

\r is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line.

The cursor is the position where the next characters will be rendered.

So, printing a \r allows to override the current line of the terminal emulator.

Whereas \b is backspace character it takes the cursor one character backward so that when you will type next thing the characters ahead of cursor will be overwritten.

Thus \b illustrates the working of backspace whereas carriage return simply moves the cursor to the starting of the current line.

Hello sir ..... plz tell me about backspace how to use it in C++. I have a program in which we want to enter user name & password so if we enter wrong any of them so how to erase them..??? How to enter backspace

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.