Objects First


Class of characters

Another primitive class is that of characters.

A character literal in C is represented by the character in single quotation marks, e.g 'a', 'C', '?'. The name of the class is char, so we can declare character variables:

char p, q, r;
and assign characters to them:

p = 'a'; q = 'X'; r = p;

Operations on characters

All the operations for integers may be used with characters also. C promotes a character object to an integer before using it in an expression. The integer value is the character's code in the character set used on the particular machine. Most modern machines use the ASCII coding scheme, but some older ones use EBCDIC. By referring to a table of the ASCII codes, it becomes obvious whether an expression like:

'a' < 'A'

will produce true or false. You can also use the table to work out what expressions like:

'b' + 10

will produce.

Key terms

promotes
The automatic conversion by the compiler of an object of one type to another type. C always promotes char to int in expressions. It also promotes int to double or float in mixed mode expressions.

Continue on to Floating Point Classes
Back to the Table of Contents
© John Morris, 1998