Previous Table of Contents Next


A character constant consists of a character surrounded by single quotes (also known as apostrophes): ‘A’, ‘2’, ‘ ’ (space). Several character escape sequences are available; all begin with the backslash (\), and all represent one character even though they occupy two (or more) characters in the constant. The character escape sequences are as follow:

\a “alert” or bell
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\’ single quote (apostrophe)
\” double quote
\\ backslash
\0 the “null character”; the character with the value 0 (used as a string terminator; see below)
\nnn the character with the octal value nnn (1–3 octal digits)
\xnn the character with the hexadecimal value nn (1 or more hexadecimal digits)

For example, the constants ‘\n’, ‘\177’, ‘\xff’, and ‘\’’ are all character constants, consisting of a newline character, the character with value 0177 (127 decimal), the character with value 0xff (255 decimal), and a single quote, respectively.

A character constant has type int. (This represents one significant difference between C and C++.)

It is theoretically permissible to place multiple characters within one character constant, as in ‘ab’, although the result is implementation-defined and of questionable utility.

C has no formal string type, but there is considerable support in the language for a particular representation of strings: the characters in the string are simply stored consecutively in an array of char. The actual length of the string (which is often less than the size of the array) is recorded by terminating the string with a special character. The character with the value 0 is reserved for this purpose; it is represented as \0 (which is, of course, a special case of the \nnn notation). This terminating character is also called the “null character.”

The language supports constants for these strings, termed string literals (or, informally, simply string constants). The value of a string literal is an unnamed, unmodifiable array containing the requested characters and the terminating \0, which the compiler automatically appends. Examples of valid string constants are “Hello, world!”, “abc”, “A”, and “” (the latter being the empty string, sometimes called the null string, and consisting solely of the terminating null character). Adjacent string literals are concatenated at compile time; for example, “ab” “cd” results in “abcd”. (This concatenation is useful when extremely long strings must be split across source lines.)

When you are working with wide characters, an L may be prepended to a character constant to give it type wchar_t, or to a string literal to give it type “array of wchar_t.” Examples are L’A’ and L”abc” (or, depending on the sophistication of the development environment, L’æ’ or L”ïöü”).

Notice that character constants and string constants are distinct, even when a particular string constant happens to contain a single character. That is, ‘A’ and “A” are very different (the latter is an array containing two characters, A and \0). Also, notice that although characters are represented internally as small integers that are the characters’ values in the machine’s character set, a digit character typically does not have its own value; the constants 2 and ‘2’ are very different (the latter has the value 50 in ASCII). Finally, a string containing digits is different from the number it might seem to represent; the string “123” is not the number 123 (although C’s atoi function would convert the string “123” to the integer 123; see section 3.10.4.2.1).

3.2.3. Declarations

As mentioned previously, C is strongly typed and requires that all variables be declared before use. The syntax of a simple declaration is

type identifier ;

where type is one of the basic types from section 3.2.1 or one of the user-defined types described in sections 3.2.9 and 3.7, and identifier is the name of the variable to be declared. For example,

int i;

declares an int variable named i, and

double degreescentigrade;

declares a double variable named degreescentigrade.

It is also possible to declare several variables, of the same base type, within the same declaration; multiple identifiers are separated by commas:

int j, k;

Furthermore, it is also possible to specify an initial value (an initializer):

float f1 = 1.2;
double d1 = 3.4, d2 = 5.6;

The initializer can be one of the constants from section 3.2.2, or a more general expression. (Depending on the particular variable being declared, the expression may have to be a constant expression as described in section 3.3.12. See also section 3.2.8.)

Rather than a simple identifier, a declaration can include a more complex declarator which indicates that an array or some other derived type is being declared rather than a simple variable. Later sections describe the additional type qualifiers and storage-class specifiers that can be used to adjust the meaning of a declaration. A more complete skeletal syntax for a declaration, incorporating all of these possibilities, is

[sc] [qualifier] type declarator [=init] [, declarator [=init]…] ;

where sc is the storage-class and brackets ([ ]) surround optional elements. (Actually, there is considerably more freedom in the arrangement of the sc, qualifier, and type than is indicated in the above skeleton, but placing the sc in other than the first position is discouraged.)


Previous Table of Contents Next