| | |
What is the purpose of using "\r" and "\b" in C++?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 622
Reputation:
Solved Threads: 82
0
#2 Oct 17th, 2009
See: http://msdn.microsoft.com/en-us/libr...bw(VS.80).aspx
For the implementations of \n and \r\n on different systems (Win vs. *nix, and others) see: http://en.wikipedia.org/wiki/Newline
But basically,
\b is a backspace and \r is carriage return
For the implementations of \n and \r\n on different systems (Win vs. *nix, and others) see: http://en.wikipedia.org/wiki/Newline
But basically,
\b is a backspace and \r is carriage return
0
#3 Oct 17th, 2009
"\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
"\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
0
#4 Oct 17th, 2009
•
•
•
•
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.
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. Last edited by tux4life; Oct 17th, 2009 at 2:32 pm. Reason: add link to full table of supported backslash codes
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
0
#5 Oct 17th, 2009
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:
and place '\a' '\b' '\c' ... and so on and test what every character does.
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:
c++ Syntax (Toggle Plain Text)
cout << "TestString1__TestString2";
Last edited by csurfer; Oct 17th, 2009 at 3:27 pm.
I Surf in "C"....
![]() |
Similar Threads
- What does "->" do or mean in PHP? (PHP)
- Taking Values from another form to Table layout panel ("How?") (VB.NET)
- A "How do you " question (C++)
- PHP "hello" in different languages ??? (PHP)
- using a "for" loop to read a text file (VB.NET)
- google "keyword" question (Search Engine Optimization)
- body.onload += "XX" (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Declaring a variable with Multiple data types
- Next Thread: crash on exit
Views: 310 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






