Character Constants and String Literals
A character constant consists of one or more characters enclosed in single quotes. Some examples are:
'0' 'A' 'ab'
Character constants have type int. The value of a
character constant that contains one character
is the numerical value of the representation of the character. For
example, in the ASCII code, the character constant
'0' has the value 48, and the constant
'A' has the value 65.
The value of a character constant that contains more than one character is dependent on the given implementation. To ensure portability, character constants with more than one character should be avoided.
Escape sequences such as
'\n' may be used in character constants. The
characters ' and \ can also be
represented this way.
The prefix L can be used to give a character
constant the type wchar_t; for example:
L'A' L'\x123'
A string literal consists of a sequence of characters and escape sequences enclosed in double quotation marks; for example:
"I am a string!\n"
A string literal is stored internally as an array of
char (see Section 1.10)
with the string terminator '\0'. It is therefore
one byte longer than the specified character sequence. The empty
string occupies exactly one byte. A string literal is also called a
string constant, although the memory it occupies
may be modified.
The string literal "Hello!", for example, is
stored as a char array, as shown in Figure 1-3.
String literals that are separated ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access