Print Friendly and PDF
(Integer Equivalent of a Character) 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 considerable 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 character in single quotes, as with
(Integer Equivalent of a Character) 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 considerable 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 character 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
 
This is called a cast operation (we formally introduce casts in Chapter 4). 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, digits and special characters (like $).
Solution: 

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int main()
{
   cout << 'A' << " = " << static_cast< int >( 'A' ) << endl
      << 'B' << " = " << static_cast< int >( 'B' ) << endl
      << 'C' << " = " << static_cast< int >( 'C' ) << endl
      << 'a' << " = " << static_cast< int >( 'a' ) << endl
      << 'b' << " = " << static_cast< int >( 'b' ) << endl
      << 'c' << " = " << static_cast< int >( 'c' ) << endl
      << '0' << " = " << static_cast< int >( '0' ) << endl
      << '1' << " = " << static_cast< int >( '1' ) << endl
      << '2' << " = " << static_cast< int >( '2' ) << endl
      << '$' << " = " << static_cast< int >( '$' ) << endl
      << '*' << " = " << static_cast< int >( '*' ) << endl
      << '+' << " = " << static_cast< int >( '+' ) << endl
      << '/' << " = " << static_cast< int >( '/' ) << endl
      << ' ' << " = " << static_cast< int >( ' ' ) << endl;
 
   return 0;
}
 
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: