![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Double quotes represent strings; single quotes represent single characters. As the tab character is just that - a single character - both will work.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|