Character and string literals
There also exist non-numerical constants, like:
'z'
'p'
"Hello world"
"How do you do?" |
When writing both single character and string literals, it is necessary to put the quotation marks surrounding them to distinguish them from possible variable identifiers or reserved keywords. Notice the difference between these two expressions:
x
'x'
|
Character and string literals have certain peculiarities, like the escape codes. These are special characters that are difficult or impossible to express otherwise in the source code of a program, like newline (\n) or tab (\t). All of them are preceded by a backslash (\). Here you have a list of some of such escape codes:
\n
|
Newline
|
\r
|
carriage return
|
\t
|
Tab
|
\v
|
vertical tab
|
\b
|
Backspace
|
\f
|
form feed (page feed)
|
\a
|
alert (beep)
|
\'
|
single quote (')
|
\"
|
double quote (")
|
\?
|
question mark (?)
|
\\
|
backslash (\)
|
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree" |
String literals can extend to more than a single line of code by putting a backslash sign (\) at the end of each unfinished line.
"string expressed in \
two lines" |
"this forms" "a single" "string" "of characters" |
L"This is a wide character string" |
No comments:
Post a Comment