-
December 3rd, 2011, 01:23 PM
#1
escape sequence and multi character interpretation
Hi
I believe "\" signals that the next character is to be interpreted differently than how it's normally dealt with. Can "\" only be used to signal the alternative interpretation of a signal character at a time? You see, in the code below, the statement in red gives rise to problem. Please help me with it. Thanks.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Nancy said, \"This is backslash t, \t, OK\"" << endl;
cout << "Nancy said, \"This is backslash t, \\t, OK\"" << endl;
cout << "feet" << "\'-" << "inches" << "\"" << endl;
cout << "feet" << '\'' << "-" << "inches" << '\"' << endl;
cout << "feet" << '\'-' << "inches" << '\"' << endl; //problem
system("pause;");
return 0;
}
-
December 3rd, 2011, 03:23 PM
#2
Re: escape sequence and multi character interpretation
Yes, it's the part in red that causes the problem, but it's not due to an invalid escape sequence. The construct in red is a multicharacter constant and evaluates to an int, and that's wat you get as output: a number. Character literals that are meant to result in a value of type char can only contain a single character (where an escape sequence counts as a single character).
Originally Posted by heights
Can "\" only be used to signal the alternative interpretation of a signal character at a time?
I'm not sure I completely understand your question, but perhaps this is an adequate answer: An escape sequence always consists of a backslash and one other character (where the other character may be a backslash itself) except for those escape sequences specifying a hexadecimal or octal character code.
For reference: C++ Character Constants
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
December 3rd, 2011, 03:29 PM
#3
Re: escape sequence and multi character interpretation
Yes it can only operate on a single character at a time.
-
December 3rd, 2011, 03:35 PM
#4
Re: escape sequence and multi character interpretation
there are only 14 escape sequences :
Newline \n
Horizontal tab \t
Vertical tab \v
Backspace \b
Carriage return \r
Formfeed \f
Alert \a
Backslash \\
Question mark \?
Single quotation mark \'
Double quotation mark \"
Octal number \ooo
Hexadecimal number \xhhh
Null character \0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|