3.8 Storage of Basic Types

Table 3.2 summarizes the storage associated with each basic type.

Table 3.2 Sizes of Fundamental Types

Type 16-Bit Targets 32-Bit Targets

char, unsigned char, signed char 1 byte 1 byte
short, short int, signed short, unsigned short 2 bytes 2 bytes
int, unsigned int, signed int 2 bytes 4 bytes
long, unsigned long, signed long 4 bytes 4 bytes
float 4 bytes 4 bytes
double 8 bytes 8 bytes
long double 10 bytes 10 bytes

The C data types fall into general categories. The “integral types” include char, int, short, long, signed, unsigned, and enum. The “floating types” include float, double, and long double. The “arithmetic types” include all floating and integral types.

Type char

The char type is used to store the integer value of a member of the representable character set. That integer value is the ASCII code corresponding to the specified character.

Microsoft Specific

Character values of type unsigned charhave a range from 0 to 0xFFh. A signed charhas range 0x80h to 0x7Fh. These ranges translate to 0 to 255 decimal, and –128 to +127, decimal, respectively. The /J command-line option changes the default from signedto unsigned.¨Type int

The size of a signed or unsigned int item is the standard size of an integer on a particular machine. For example, on a 16-bit computer, the int type is usually 16 bits, or 2 bytes. On a 32-bit machine, the int type is usually 32 bits, or 4 bytes. Thus, the int type is equivalent to either the short int or the long int type, and the unsigned int type is equivalent to either the unsigned short or the unsigned long type, depending on the target environment. The int types all represent signed values unless specified otherwise.

The type specifiers int and unsigned int (or simply unsigned) define certain features of the C language (for instance, the enum type). In these cases, the definitions of int and unsigned int for a particular implementation determine the actual storage.

Microsoft Specific

Signed integers are represented in two's-complement form. The most-significant bit holds the sign: 1 for negative, 0 for positive and zero. The range of values is given in Table 1.3, which is taken from the LIMITS.H header file.¨

Note:

The int and unsigned int type specifiers are widely used in C programs because they allow a particular machine to handle integer values in the most efficient way for that machine. However, since the sizes of the int and unsigned int types vary, programs that depend on a specific int size may not be portable to other machines. To make programs more portable, you can use expressions with the sizeof operator (as discussed in “The sizeof Operator”) instead of hard-coded data sizes.

Type float

Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. The mantissa represents a number between 1.0 and 2.0. Since the high-order bit of the mantissa is always 1, it is not stored in the number. This representation gives a range of approximately 3.4E–38 to 3.4E+38 for type float.

Microsoft Specific

The floattype contains 32 bits: 1 for the sign, 8 for the exponent, and 23 for the mantissa. Its range is +/–3.4E38 with at least 7 digits of precision.¨Type double

Double precision values with double type have 8 bytes. The format is similar to the float format except that it has an 11-bit excess-1023 exponent and a 52-bit mantissa, plus the implied high-order 1 bit. This format gives a range of approximately 1.7E–308 to 1.7E+308 for type double.

Microsoft Specific

The doubletype contains 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/–1.7E308 with at least 15 digits of precision.¨Type long double

The range of values for a variable is bounded by the minimum and maximum values that can be represented internally in a given number of bits. However, because of C's conversion rules (discussed in detail in “Type Conversions”) you cannot always use the maximum or minimum value for a constant of a particular type in an expression.

For example, the constant expression -32768 consists of the arithmetic negation operator () applied to the constant value 32,768. Since 32,768 is too large to represent as a short int, it is given the long type. Consequently, the constant expression -32768 has long type. You can only represent –32,768 as a short int by type-casting it to the short type. No information is lost in the type cast, since –32,768 can be represented internally in 2 bytes.

The value 65,000 in decimal notation is considered a signed constant. It is given the long type because 65,000 does not fit into a short. A value such as 65,000 can only be represented as an unsigned short by type-casting the value to unsigned short type, by giving the value in octal or hexadecimal notation, or by specifying it as 65000U. You can cast this long value to the unsigned short type without loss of information, since 65,000 can fit in 2 bytes when it is stored as an unsigned number.

Microsoft Specific

The long doublecontains 80 bits: 1 for sign, 15 for exponent, and 64 for mantissa. Its range is +/–1.2E4932 with at least 17 digits of precision.¨