This is called a cast operation. When the preceding statement executes, it prints the value 65 (on systems that use the ASCII character set). Write a program that prints the integer equivalent of a character typed at the keyboard. Store the input in a variable of type char. Test your program several times using uppercase letters, lowercase letters, dig- its and special characters (like $).

Here is a peek ahead. In this chapter you learned about integers and the type int. C++ can also represent uppercase letters, lowercase letters and a consider-
able variety of special symbols. C++ uses small integers internally to represent each different character. The set of characters a computer uses and the corresponding integer representations for those characters are called that computer’s character set. You can print a character by enclosing that char-
acter in single quotes, as with
cout << 'A'; // print an uppercase A
You can print the integer equivalent of a character using static_cast as follows:
cout << static_cast< int >( 'A' ); // print 'A' as an integer

```
#include // allows program to perform input and output
using namespace std; // program uses names from the std namespace

int main()
{
char symbol; // char read from user

cout << "Enter a character: "; // prompt user for data
cin >> symbol; // read the character from the keyboard

cout << symbol << "'s integer equivalent is "
<< static_cast< int >( symbol ) << endl;
} // end main
```
Enter a character: A
A's integer equivalent is 65
Enter a character: B
B's integer equivalent is 66
Enter a character: a
a's integer equivalent is 97
Enter a character: 7
7's integer equivalent is 55
Enter a character: $
$'s integer equivalent is 36

Computer Science & Information Technology

You might also like to view...

You would adjust the ________ to control the lightness or darkness of a picture

Fill in the blank(s) with correct word

Computer Science & Information Technology

Saturation is the amount of color that is reflected from an object.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology