Tuesday, September 21, 2010

The Five Basic Data Types

There are five atomic data types in C: character, integer, floating-point, double
floating-point, and valueless (char, int, float, double, and void, respectively). As you
will see, all other data types in C are based upon one of these types. The size and range
of these data types may vary between processor types and compilers. However, in all
cases a character is 1 byte. The size of an integer is usually the same as the word length
of the execution environment of the program. For most 16-bit environments, such as
DOS or Windows 3.1, an integer is 16 bits. For most 32-bit environments, such as
Windows NT, an integer is 32 bits. However, you cannot make assumptions about
the size of an integer if you want your programs to be portable to the widest range of
environments. It is important to understand that both C and C++ only stipulate
the minimal range of each data type, not its size in bytes.
 
**To the five basic data types defined by C, C++ adds two more: bool and wchar_t.
These are discussed in Part Two.

The exact format of floating-point values will depend upon how they are
implemented. Integers will generally correspond to the natural size of a word on the
host computer. Values of type char are generally used to hold values defined by the
ASCII character set. Values outside that range may be handled differently by different
compilers.
The range of float and double will depend upon the method used to represent
the floating-point numbers. Whatever the method, the range is quite large. Standard
C specifies that the minimum range for a floating-point value is 1E−37 to 1E+37. The
minimum number of digits of precision for each floating-point type is shown in
Table 2-1.

**Standard C++ does not specify a minimum size or range for the basic types. Instead,
it simply states that they must meet certain requirements. For example, Standard
C++ states that an int will “have the natural size suggested by the architecture of
the execution environment." In all cases, this will meet or exceed
the minimum ranges specified by Standard C. Each C++ compiler specifies the size
and range of the basic types in the header <climits>.

Type                       Typical Size in Bits                       Minimal Range
char                                       8                                      −127 to 127
unsigned char                         8                                         0 to 255
signed char                            8                                        −127 to 127
int                                         16                                  or 32 −32,767 to 32,767
unsigned int                           16 or 32                              0 to 65,535
signed int                             16 or 32                               same as int
short int                                16 −32,                              767 to 32,767
unsigned short int                 16                                         0 to 65,535
signed short int                     16                                        same as short int
long int                                 32                                       −2,147,483,647 to2,147,483,647
signed long int                      32                                        same as long int
unsigned long int                  32                                       0 to 4,294,967,295
float                                    32                                        Six digits of precision
double                                64                                        Ten digits of precision
long double                        80                                             Ten digits of precision 

Table 2-1. All Data Types Defined by the ANSI/ISO C Standard


No comments:

Post a Comment