Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 2:37 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
About using escape sequences

I found out that in order to use an escape sequence you must enclose it within either double quotes or single quotes like this:

cout << number << "\t" << endl;

or

cout << number << '\t' << endl;

If you do this the compiler will complain:

cout << number\t << endl;

I guess what I'm asking is the why of it(I'd like to know as much as possible cuz I always document my programs). Why for instance must you enclose the escape sequence in quotes? And why do both "\t" and '\t' work?
357mag is offline   Reply With Quote
Old Apr 26th, 2005, 3:25 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
An escape sequence is simply a character literal. Single quotes make a character literal stand-alone, and double quotes make a character part of a string literal.

>cuz I always document my programs
Documenting well known language features serves no purpose in production code. For example, '\t' is well known to be an escape character representing a tab. No comments are needed to explain that. On the other hand, something like '\003' would need explanation. It's a character with the value 3, but what does the character represent in this situation? That is what you need to document, not how it works, but what it means in the application.
Eggbert is offline   Reply With Quote
Old Apr 26th, 2005, 3:55 PM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Thanks for your explanation. I was aware that \t was an escape sequence. I'm not writing a comment just for that, but I wanted to know the why of the single quotes or double quotes.
357mag is offline   Reply With Quote
Old Apr 26th, 2005, 4:37 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Double quotes represent strings; single quotes represent single characters. As the tab character is just that - a single character - both will work.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC