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:
char chValue = 'a'; char chValue2 = 97;
cout outputs char type variables as characters instead of numbers.
The following snippet outputs ‘a’ rather than 97:
char chChar = 97; // assign char with ASCII code 97 cout << chChar; // will output 'a'
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:
char chChar = 97; cout << (int)chChar; // will output 97, not 'a'
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:
#include "iostream";
int main()
{
using namespace std;
char chChar;
cout << "Input a keyboard character: ";
cin >> chChar;
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
char chValue = '5'; // assigns 53 (ASCII code for '5') char chValue2 = 5; // assigns 5
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:
#include <iostream>
int main()
{
using namespace std;
cout << "First line\nSecond line" << endl;
return 0;
}
This outputs:
First line Second line
Another commonly used escape sequence is ‘\t’, which embeds a tab:
#include <iostream>
int main()
{
using namespace std;
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 |
2.8 — Constants
|
Index
|
2.6 — Boolean Values
|
2.8 — Constants
Index
2.6 — Boolean Values
[ Question answered in the forum. -Alex ]
Alex:
Ref.: cout << “First line\\nSecond line” << endl;
Why the second ‘\’ for ‘\n’ ?
Thanks,
Æ’ree
It was a typo. It’s fixed now. Thanks for noticing!
Does that also apply to this?
cout << “First part\\tSecond part”;
Thanks for a great tutorial
Yup, that tab too. :) Thanks for noticing.