Fundamental types in C++ are divided into three categories: “integral,” “floating,” “void,” and “segment.” Integral types are capable of handling whole numbers, while floating types are capable of specifying values that may have fractional parts.
The void type describes an empty set of values. No variable of type void may be specified—it is primarily used in declaring functions that return no values or in declaring “generic” pointers to untyped or arbitrarily typed data. Any expression can be explicitly converted or cast to type void. However, such expressions are restricted to the following uses:
An expression statement. (See Chapter 4, “Expressions,” for more information.)
The left operand of the comma operator. (See “The Comma Operator” in Chapter 4, on topic for more information.)
The second or third operand of the conditional operator (? :). (See “Expressions with the Conditional Operator” in Chapter 4, on topic for more information.)
The __segment type is used only when specifying the segment for a based object or pointer.
Table 2.3 explains the restrictions on type sizes. These restrictions are independent of the Microsoft implementation.
Table 2.3 Fundamental Types of the C++ Language
Category | Type | Contents |
Integral | char | Type char is an integral type that usually contains members of the execution character set—in Microsoft C++, this is ASCII. |
Variables of type char may be declared as signed char or unsigned char; in either case, they are the same size as a variable declared simply as type char. The C++ compiler treats variables of type char, signed char, and unsigned char as having different types. Variables of type char are treated as type signed char by default, unless the /J compilation option is used, in which case they are treated as type unsigned char. | ||
short | Type short int (or simply short) is an integral type that is larger than or equal to the size of type char, and shorter than or equal to the size of type int. | |
Objects of type short may be declared as signed short or unsigned short; in either case, they are the same size as an object declared simply as type short. The C++ compiler treats objects of type short and signed short as different from unsigned short . |
Table 2.3 (continued)
Category | Type | Contents |
int | Type int is an integral type that is larger than or equal to the size of type short int and shorter than or equal to the size of type long. | |
Objects of type int may be declared as signed int or unsigned int; in either case, they are the same size as an object declared simply as type int. The C++ compiler treats objects of type int and signed int as different from unsigned int. | ||
long | Type long (or long int) is an integral type that is larger than or equal to the size of type int. | |
Objects of type long may be declared as signed long or unsigned long; in either case, they are the same size as objects declared simply as type long. The C++ compiler treats objects of type long and signed long as different from unsigned long. | ||
Floating | float | Type float is the smallest floating type. |
double | Type double is a floating type that is larger than or equal to type float but shorter than or equal to the size of type long double. | |
long double | Type long double is a floating type that is larger than or equal to type double. |
In Microsoft C++, variables of various fundamental types require different amounts of storage, depending on whether the program is compiled for a 16- or 32-bit target. Table 2.4 shows these differences.
Table 2.4 Sizes of Fundamental Types
Type | 16-bit Target Compilation | 32-bit Target Compilation |
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, long int, unsigned long, signed long, 4 bytes | 4 bytes | ||
float | 4 bytes | 4 bytes | |
double | 8 bytes | 8 bytes | |
long double | 10 bytes | 10 bytes |
For more information about type conversion, see Chapter 3, “Standard Conversions.”