THE char TYPES
In C++, the character type char is one of the integer types. This means that any variable of type char may be used in integer expressions just like any other integer. For example, the integer arithmetic operators apply to char variables:
char c = 54;
char d = 2*c – 7;
c += d%3;
The name “char” is short for “character”. The name char is used because when variables of this type are input or output, they are interpreted as characters. Whenever a character is input, the system automatically stores its ASCII code (see Appendix A) as the value of the integer type char. And whenever a variable of type char is output, the system automatically sends the corresponding character to the output stream. This illustrated in the example below.
C++ defines three 8-bit integer types: char, signed char, and unsigned char. But only two of these are distinct. The type char will be either signed char or unsigned char, depending upon the computer. Use type char for ordinary characters. Use the type unsigned char for very short bit-string. the type signed char is not explicitly used very often; it would be a good choice if you needed to store a large quantity of very short integers that would not have to be output by means of the standard output operator <<.
EXAMPLE: Character Output
This shows how char variables are output:
// Tests output of type char:
char c = 64;
cout << c++ << “ “; // prints ‘@ ’ and increments c to 65
cout << c++ << “ “; // prints ‘A ’ and increments c to 66
cout << c++ << “ “; // prints ‘B ’ and increments c to 67
cout << c++ << end1; // prints ‘C ’ and increments c to 68
c = 96;
cout << c++ << “ “; // prints ‘ ‘ ’ and increments c to 97
cout << c++ << “ “; // prints ‘a ’ and increments c to 98
cout << c++ << “ “; // prints ‘b ’ and increments c to 99
cout << c++ << end1; // prints ‘c ’ and increments c to 100
@ A B C
‘ a b c
The first output statement inserts the character variable c into the output stream. Since it has the integer value 64, it is output as the character “@”. (The ASCII code for the “at” symbol is 64). Then c is immediately to 65 which causes the character “A” to be output next. (The ASCII code for the letter A is 65). The rest of the program continues in a similar way. (Note that on computers that use the EBCDIC code, the output here will be different).
EXAMPLE: Obtaining the ASCII Code
// Tests output of type char:
char c = ‘ A ‘ ;
cout << c++ << “ “ << int (c) << end1; // prints ‘A’ and 65
cout << c++ << “ “ << int (c) << end1; // prints ‘B’ and 66
cout << c++ << “ “ << int (c) << end1; // prints ‘C’ and 67
As this program executes, c takes the values 65, 66, 67, and 68. But since character variables are printed as characters, the first thing printed on each line is the character whose ASCII code is stored in c. Thus, A, B,and C are printed. We use int (c) to print the numeric value of the character c.
The expression int (c) is called a cast. It converts c from char type to int type. This allows us to print the ASCII code of a character.
Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond
——————– Thanks everyone