|
|
C and C++ offer the programmer a rich assortment of built-in data types. Programmer-defined data types can be created to fit virtually any need. Variables can be created for any valid data type. Also, it is possible to specify constants of C/C++’s built-in types. In this section, various features relating to data types, variables, and constants are discussed.
C89 defines the following elemental data types:
Type | Keyword |
---|---|
Character | char |
Integer | int |
Floating point | float |
Double floating point | double |
Valueless | void |
To these, C99 adds the following:
Type | Keyword |
---|---|
Boolean (true/false) | _Bool |
Complex | _Complex |
Imaginary | _Imaginary |
C++ defines the following basic types:
Type | Keyword |
---|---|
Boolean (true/false) | bool |
Character | char |
Integer | int |
Floating point | float |
Double floating point | double |
Valueless | void |
Wide character | wchar_t |
As you can see, all versions of C and C++ provide the following five basic types: char, int, float, double, and void. Also notice that the keyword for the Boolean type is bool in C++ and _Bool in C99. No Boolean type is included in C89.
Several of the basic types can be modified using one or more of these type modifiers:
signed
unsigned
short
long
The type modifiers precede the type name that they modify. The basic arithmetic types, including modifiers, allowed by C and C++ are shown in the following table along with their guaranteed minimum ranges. Most compilers will exceed the minimums for one or more types. Also, if your computer uses two’s complement arithmetic (as most do), then the smallest negative value that can be stored by a signed integer will be one more than the minimums shown. For example, the range of an int for most computers is –32,768 to 32,767. Whether type char is signed or unsigned is implementation dependent.
Type | Minimum Range |
---|---|
char | –127 to 127 or 0 to 255 |
unsigned char | 0 to 255 |
signed char | –127 to 127 |
int | –32,767 to 32,767 |
unsigned int | 0 to 65,535 |
signed int | same as int |
short int | same as int |
unsigned short int | 0 to 65,535 |
signed short int | same as short int |
long int | –2,147,483,647 to 2,147,483,647 |
signed long int | same as long int |
unsigned long int | 0 to 4,294,967,295 |
long long int | –(263–1) to 263–1 (C99 only) |
signed long long int | same as long long int (C99 only) |
unsigned long long int | 0 to 264–1 (C99 only) |
float | 6 digits of precision |
double | 10 digits of precision |
long double | 10 digits of precision |
wchar_t | same as unsigned int |
When a type modifier is used by itself, int is assumed. For example, you can specify an unsigned integer by simply using the keyword unsigned. Thus, these declarations are equivalent.
unsigned int i; // here, int is specified unsigned i; // here, int is implied
|
|