Previous Table of Contents Next


Running a close second in popularity to the integer types is the character type because C is often used for text processing. Characters are stored as small integers, using the mapping defined by the machine’s native character set. Characters can, therefore, be thought of as tiny integer values, and they are not uncommonly used for this purpose. The basic character type is char. As for the integers, the modifier keywords signed and unsigned can be used to explicitly request signed or unsigned characters, but unlike the integers, a plain char can be signed or unsigned, depending on the implementation’s choice. The keyword signed can, therefore, be significant in the declaration of a character object. Characters have a guaranteed minimum range of ±127 if they are signed, and 0–255 if they are unsigned. In any case, the characters in C’s basic source and basic execution sets (roughly speaking, the 52 upper- and lowercase English letters, the 10 digits, and the 29 punctuation characters used in C) are all guaranteed to have positive values (as they do in ASCII). However, any extended characters (such as those in the ISO8859-1 Latin-1 character set, or the extended character sets of MS-DOS or Macintosh systems) are likely to have values that appear negative if characters are signed.

To summarize, then, the four integral types, and their guaranteed minimum ranges in both their signed and unsigned variants, are as follow:

signed unsigned

char ±127 0–255
short ±32,767 0–65,535
int ±32,767 0–65,535
long ±2,147,483,647 0–4,294,967,295

The integral types have the widest range of supported operations, including arithmetic, comparison, and “bitwise” operators, all of which are covered in later sections.

C also has three floating-point types: the single-precision float, the double-precision double, and the extended-precision long double. The absolute minimum range of these types is ±1037, with the equivalent of at least 6 decimal digits’ worth of precision for type float, and 10 digits for double and long double. Most machines provide more range and/or precision (but note that type long double is not in fact guaranteed to have any more precision or range than plain double).

The allowable operations on floating-point values are somewhat fewer than for integral variables; only arithmetic operations and comparisons are supported.

Finally, C has one “placeholder” type, void, which is used in a few situations in which a type is required but no values are expected. In the mathematical sense, type void has no values in its range and supports no operations. Type void is used in the parameter list of functions that accept no parameters, as the return value of functions that return no values (i.e., “procedures”), as an explicit indication that the result of an expression is being discarded, and in its pointer form as a “generic” pointer type (i.e., when the actual type pointed to is not precisely known). All of these uses are discussed later.

In addition to these basic types, there are any number of auxiliary and derived types, some defined by the language and some defined by the programmer. One that deserves mention here is wchar_t, which is a type that can hold wide characters, characters from extended or multinational character sets that would not fit in a char.

3.2.2. Constants

Source-code constants take several forms, roughly paralleling the several basic types listed in the preceding section.

Integer constants are indicated in the obvious way by simple digit sequences: 0, 1, 23, 456. Integer constants are in decimal (base 10) by default, but may be entered in octal (base 8) by prepending a leading 0 (digit zero), or in hexadecimal (base 16) by prepending a leading 0x or 0X. (The hexadecimal digits are 09 and the letters af or AF). Thus, 010 and 0377 are octal constants, and 0x10, 0x1abc, and 0XFEED are hexadecimal constants. The constants 100, 0144, and 0x64 all have the same value, one hundred decimal.

Integer constants are of type int by default, unless they are too large to be represented in a plain int on the particular machine, in which case they are automatically assumed to be long and/or unsigned, as necessary. (Octal and hexadecimal constants have a preference for unsigned.) It is occasionally necessary to force a constant to be of a certain type; this can be done with the suffixes l (lowercase letter l) or L for long, and u or U for unsigned. So 1L is explicitly long, 2u is explicitly unsigned, and 3ul is explicitly unsigned long.

A decimal-point character (.) indicates a floating-point constant, as in 3.14. The letter e or E may also be used to indicate exponential notation: 1.23e4 is 1.23×104, or 12,300. A constant is floating point if it includes a decimal point (.) or e (or E) or both; digits may precede or follow the decimal point, or both. A sign (+ or -) may follow the letter e; 1.23e-4 is 1.23×10-4, or 0.000123.

Floating-point constants are of type double by default. A suffix f or F may be appended to force type float, or l (again, letter l) or L may be suffixed to indicate long double.


Previous Table of Contents Next