5.3.4. Characters


Characters

You can think of a character as something that you can type on a keyboard. For example, 'a' is the lower case letter a and 'B' is an upper case B. A character has type char.

Each character has in integer code. For example, the code for 'a' is 97 and the code for 'B' is 66. The codes for upper case letters are consecutive, starting with 'A' = 65, and the codes for the lower case letters are also consecutive, starting with 'a' = 97.

In C++, characters are actually treated very much like their integer codes. For example, you can perform arithmetic on them; expression 'a' + 1 yields 98, which is the code for 'b'. You can store an integer into a variable of type char. Programmers often use type char for very small integers (from −128 to 127); a variable of type char is an 8-bit integer. A variable of type unsigned char can hold any integer from 0 to 255.


Character constants

'A'

Write characters in single-quotes.

'0'

The digits '0', '1', ... have consecutive codes 48, 49, ... Notice that the code for '0' is not 0.

'\n'

This is the newline character (code 10). It indicates the end of a line.

'\t'

This is the tab character.

'\0'

This is the null character, the character whose integer code is 0.

'\r'

This is the carriage-return character (code 13).

'\\'

This is the backslash character. Be sure to double it.

'\"'

This is the double-quote character.

Operations on Characters

To use the functions in the following table you will need to #include <cctype>. Note: Functions such as isdigit and isalpha can return integer results other than 0 or 1, with 0 meaning false and any other integer meaning true.

c − '0'

Use this to convert a digit to the number that it represents. For example, '8' − '0' yields 8.

d + '0'

Use this to convert a single-digit number d to a character. For example, 8 + '0' yields '8'.

c − ('a' − 'A')

This is the result of converting letter c from lower case to upper case. For example, 'q' − ('a' − 'A') yields 'Q'. This only works if c is a lower case letter.

c + ('a' − 'A')

This is the result of converting letter c from upper case to lower case. For example, 'Q' + ('a' − 'A') yields 'q'. This only works if c is an upper case letter.

(int)(c)

This is character c treated as having type int.

(char)(n)

This is integer n treated as having type char.

isdigit(c)

This is nonzero if c is a digit.

isalpha(c)

This is nonzero if c is a letter

isspace(c)

This is nonzero if c is a white-space character (space, tab, newline, carriage-return).

isprint(c)

Not all characters are intended to be shown. Some are special characters that are used for special purposes. isprint(c) is nonzero if c is printable; that is, if c is not a special character.

Exercises

  1. Write an expresion that is true (or nonzero) if character c is a letter. Answer

  2. Write an expresion that is true if character c is an upper case letter. Answer

  3. Suppose that c is a digit (one of the characters '0', '1', ..., '9'). Write an expression that yields the integer that corresponds to that digit. For example, if c is '4' then your expression should yield 4. Answer

  4. Look at the coding standards for writing constants. Suppose that c has type char.

      if(c == 10)
      {
        ...
      }
    
    acceptable by the coding standards? If not, how should it be written? Answer