11.1: Character and String Constants
- Page ID
- 54304
A character is a single character or symbol, typically enclosed in quotes. For example, letters (“A”-”Z” and “a” - “z”), punctuation (“!”, “,”, “?”, etc.) , symbols, (“@”, “#”, “>”, etc.), and digits “1”, “2” are characters.
Some examples include:
"X" "z" "5"
Character and string constants are case sensitive. So, character “X” (upper-case) is not the same as “x” (lower-case). When a digit is enclosed in quotes, it is treated as a character and consequently arithmetic operations (addition, subtraction, etc.) are not allowed.
A string is a series of characters. A string consists of an arbitrary sequence of characters also enclosed in quotes. Some examples include:
"Hello World." "456" "1 2 3" "456?" "Goodbye cruel world!!" "Have a nice day?"
Since digits enclosed in quotes are not numeric values, the strings “1 2 3” and “456?” are allowed.
A problem arises if you want to have a quote in the string itself. A double quote will be interpreted as a single within a string. The two quotes must be together (no spaces between). For example, the string:
"He said ""wow"" when he heard"
Would be displayed as
"He said "wow" when he heard"
The double-quote is sometimes referred to as an escape character. Strings and characters must be associated with the character data type.