5.3.4. 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. (Note that character constants are written in single-quote marks, not in double-quote marks.) 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


Write characters in single-quotes.


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

' '

This is the space character. Be sure to put a space between the quote marks.


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


This is the tab character.


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


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


This is the double-quote character.

'\r' (optional)

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

Operations on Characters

To use the predicates in the following table you will need to #include <cctype>.

Note: Predicates such as isdigit and isalpha can return integer results other than 0 or 1, with 0 meaning false and any other integer meaning true. The coding standard requiring all tests to yield either true or false are relaxed for these expressions.

Note: isdigit and isalpha are actually pseudo-functions. They are converted into expressions that select a value from an array. It is important for an array index not to be negative. So convert a character to type unsigned or unsigned char to use it as a parameter to these predicates.

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.


This is character c treated as having type int.


This is integer n treated as having type char.

isdigit((unsigned) c)

This is nonzero if c is a digit.

isalpha((unsigned) c)

This is nonzero if c is a letter

isupper((unsigned) c)

This is nonzero if c is an upper-case letter

isupper((unsigned) c)

This is nonzero if c is a lower-case letter

isspace((unsigned) c)

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


isprint((unsigned) c) is nonzero if character c can be printed and printing it uses some ink (so that you can see it). isprint(' ') is false.


  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. Is

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