differnce between escape sequence and special characters

Sep 1, 2012 at 1:04am
1) difference between escape sequences and special characters?
2) (") and (\")do these two have different meaning for the compiler?
3) uses of escape character?
Sep 1, 2012 at 7:53am
1) Depends what you mean by "special characters". Escape sequences just let you express any value as a literal string.


2) Yes, very different things.

The quote (") character is used by the compiler to differentiate between code and literal string data.

Example:

1
2
cout << "(5+2)";  // prints (5+2)
cout << (5+2); // prints 7 


Since the quote marks both the beginning and end of the string literal data, this means it would be impossible to represent the quote character inside the string normally. Example:

 
cout << "Bob said "hi" to Jeff";


As the syntax highlighting here shows, the compiler will see this as 2 different strings ("Bob said " and " to Jeff ). The 'hi' in the middle is treated as code and not part of the string... which will likely cause a compiler error.

To solve this problem, quotes can be expressed as an escape sequence:

 
cout << "Bob said \"hi\" to Jeff";


Since the inner quotes are escaped, this means they are interpreted as part of the string, rather than the begin/end markers for string data.


3) My answer to #2 illustrates one use of the escape character. Another is to express other characters which cannot be easily represented/typed.

For example \t is a tab
\n is a new line
\r is a carriage return
\\ is the \ character
\x64 is the character code represented by hex value 0x64 (lowercase 'd')
etc
Topic archived. No new replies allowed.