Even though the
char data type is an integer (and thus follows all of the normal integer rules), we typically work with chars in a different way than normal integers. Characters can hold either a small number, or a letter from the ASCII character set.
ASCII stands for American Standard Code for Information Interchange, and it defines a mapping between the keys on an American keyboard and a number between 1 and 127 (called a code). For instance, the character ‘a’ is mapped to code 97. ‘b’ is code 98. Characters are always placed between single quotes.
The following two assignments do the same thing:
cout outputs char type variables as characters instead of numbers.
The following snippet outputs ‘a’ rather than 97:
If we want to print a char as a number instead of a character, we have to tell cout to print the char as if it were an integer. We do this by using a
cast to have the compiler convert the char into an int before it is sent to cout:
The
(int) cast tells the compiler to convert chChar into an int, and cout prints ints as their actual values. We will talk more about casting in a few lessons.
The following program asks the user to input a character, then prints out both the character and it’s ASCII code:
07 | cout << "Input a keyboard character: " ; |
09 | cout << chChar << " has ASCII code " << ( int )chChar << endl; |
Note that even though cin will let you enter multiple characters, chChar will only hold 1 character. Consequently, only the first character is used.
One word of caution: be careful not to mix up character (keyboard) numbers with actual numbers. The following two assignments are not the same
Escape sequences
C and C++ have some characters that have special meaning. These characters are called
escape sequences. An escape sequence starts with a \, and then a following letter or number.
The most common escape sequence is ‘\n’, which can be used to embed a newline in a string of text:
6 | cout << "First line\nSecond line" << endl; |
This outputs:
First line
Second line
Another commonly used escape sequence is ‘\t’, which embeds a tab:
6 | cout << "First part\tSecond part" ; |
Which outputs:
First part Second part
Three other notable escape sequences are:
\’, which prints a single quote
\”, which prints a double quote
\\, which prints a backslash
Here’s a table of all of the escape sequences:
Name | Symbol | Meaning |
Alert | \a | Makes an alert, such as a beep |
Backspace | \b | Moves the cursor back one space |
Formfeed | \f | Moves the cursor to next logical page |
Newline | \n | Moves cursor to next line |
Carriage return | \r | Moves cursor to beginning of line |
Horizontal tab | \t | Prints a horizontal tab |
Vertical tab | \v | Prints a vertical tab |
Single quote | \’ | Prints a single quote |
Double quote | \” | Prints a double quote |
Backslash | \\ | Prints a backslash |
Question mark | \? | Prints a question mark |
Octal/hex number | \(number) | Translates into char represented by octal/hex number |
No comments:
Post a Comment