How Character Constants Are Stored in C
In the C programming language, character constants are enclosed in
single quotes and are internally represented not as text, but as
numerical integer values determined by the system's character set,
typically ASCII. Although they appear to represent single characters,
the C standard treats character constants as having the int
data type rather than char, meaning they occupy the memory
space of a standard integer during evaluation. This article explains the
internal encoding, type mechanics, memory storage, and handling of
special character constants in C.
Type and Value Representation
A character constant is a sequence of one or more characters enclosed
in single quotes, such as 'A' or '\n'. During
compilation, the compiler translates the character into its
corresponding numerical code from the execution character set. On modern
systems, this is almost universally the ASCII encoding standard. For
example, the character constant 'A' is directly translated
to the integer value 65.
A fundamental distinction in C—unlike C++—is that a character
constant has the type int. Consequently, the expression
sizeof('A') yields the size of an integer (typically 4
bytes on modern 32-bit and 64-bit systems), not the size of a
char (1 byte).
Internal Storage in Memory
Because character constants are typed as integers, the compiler handles them internally as standard integer literals:
- In Expressions: When evaluated in expressions, the
constant is loaded as a full-width integer. For example,
'A' + 1evaluates internally as65 + 1, performing standard integer arithmetic. - Assignment to a
char: When a character constant is assigned to acharvariable (e.g.,char c = 'A';), the integer value undergoes an implicit conversion. The compiler truncates the 4-byteintvalue to fit into the 1-byte storage allocated for thecharvariable, storing the binary equivalent of65(01000001) in an 8-bit memory location.
Escape Sequences
Non-printable characters, control codes, and reserved characters are
represented using escape sequences that begin with a backslash
(\). Internally, these resolve to specific integer values
at compile time:
- Simple escape sequences represent specific control codes, such as
'\n'(newline, ASCII value10) or'\t'(horizontal tab, ASCII value9). - Octal escape sequences (e.g.,
'\012') specify a character via a 1-to-3 digit octal number. - Hexadecimal escape sequences (e.g.,
'\x41') specify a character via hexadecimal digits, resolving directly to the indicated numerical byte value.
Regardless of notation, the internal representation remains an integer matching the specified value.
Multi-Character Constants
C permits multi-character constants, where more than one character is
placed inside single quotes, such as 'AB'.
The C standard specifies that the value of a multi-character constant
is implementation-defined. Most compilers pack the individual character
byte values into the bytes of an int. On a big-endian
system, 'AB' typically evaluates to
(65 << 8) | 66 (which equals 16706),
whereas on a little-endian system, the byte order in registers may
differ. Because their behavior is non-portable across different
compilers and architectures, multi-character constants are rarely used
in production code.