What is the purpose of using "\r" and "\b" in C++?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 4
Reputation: paruse has a little shameless behaviour in the past 
Solved Threads: 0
paruse paruse is offline Offline
Newbie Poster

What is the purpose of using "\r" and "\b" in C++?

 
0
  #1
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 622
Reputation: jonsca will become famous soon enough jonsca will become famous soon enough 
Solved Threads: 82
Sponsor
jonsca jonsca is offline Offline
Practically a Master Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: warbird43 is an unknown quantity at this point 
Solved Threads: 0
warbird43's Avatar
warbird43 warbird43 is offline Offline
Newbie Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,973
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
0
  #4
Oct 17th, 2009
Originally Posted by paruse View Post
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.
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."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training
 
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:
  1. cout << "TestString1__TestString2";
and place '\a' '\b' '\c' ... and so on and test what every character does.
Last edited by csurfer; Oct 17th, 2009 at 3:27 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 310 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC