Common Compiler-Supported C++ Variable Types
In most of the examples thus far, you have defined variables of type int—that is, integers. However, C++ programmers can choose from a variety of fundamental variable types supported directly by the compiler. Choosing the right variable type is important. An integer cannot be used to store values that contain decimals without loss of decimal data. If your program needs to store the value of pi, for instance, you can use the type float or double. Table 3.1 lists the various variable types and the data values they can contain.
TABLE 3.1 Variable Types
Type |
Values |
---|---|
bool |
true or false |
char |
256 character values |
unsigned short int |
0 to 65,535 |
short int |
–32,768 to 32,767 |
unsigned long int |
0 to 4,294,967,295 |
long int |
–2,147,483,648 to 2,147,483,647 |
unsigned long long |
0 to 18,446,744,073,709,551,615 |
long long |
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
int (16 bit) |
–32,768 to 32,767 |
int (32 bit) |
–2,147,483,648 to 2,147,483,647 |
unsigned int (16 bit) |
0 to 65,535 |
unsigned int (32 bit) |
0 to 4,294,967,295 |
float |
1.2e–38 to 3.4e38 |
double |
2.2e–308 to 1.8e308 |
long double |
2.2e–308 to 1.8e308 (This is the same as a double variable type on Microsoft Visual C++ [MSVC] but is supported differently on other platforms.) |
The following sections explain the important variable types in greater detail.
Using Type bool to Store Boolean Values
C++ provides a type bool that is specially created for containing Boolean values true or false, both of which are reserved words.
Type bool is particularly useful for storing settings and flags that can be on or off, present or absent, available or unavailable, and the like.
A sample declaration of an initialized Boolean variable is
bool alwaysOn = true;
An expression that evaluates to a Boolean type is
bool deleteFile = (userSelection == "yes"); // evaluates to true if userSelection contains "yes", else to false
Conditional expressions are explained in Lesson 5, “Working with Expressions, Statements, and Operators.”
Using Type char to Store Character Values
You use type char to store a single character. A sample declaration is
char userInput = 'Y'; // initialized char to 'Y'
Memory is composed of bits and bytes. Bits evaluate to either state 0 or state 1. Bytes are the smallest unit of memory and comprise bits: 1 byte contains 8 bits. Thus bytes contain numeric data in binary format. When a program uses character data as shown in the example above, the compiler converts the character into a numeric representation that can be placed into memory. The numeric representation of Latin characters A–Z, a–z, numbers 0–9, some special keystrokes (for example, DEL), and special characters (such as backspace) has been standardized by the American Standard Code for Information Interchange, also called ASCII.
You can look up the table in Appendix D, “ASCII Codes,” to see that the character Y assigned to variable userInput has the ASCII value 89 in decimal or 01011001 in binary. Thus, the compiler stores 01011001 in the memory space allocated for userInput.
The Concept of Signed and Unsigned Integers
Sign implies positive or negative. All numbers you work with using a computer are stored in memory in the form of bits and bytes. A memory location that is 1 byte in size contains 8 bits. Each bit can either be a 0 or 1 (that is, carry one of these two values, at best). Thus, a memory location that is 1 byte in size can contain a maximum of 2 to the power 8 values—that is, 256 unique values. Similarly, a memory location that is 16 bits in size can contain 2 to the power 16 values—that is, 65,536 unique values.
If these values were to be unsigned—that is, assumed to be only positive—then 1 byte could contain integer values ranging from 0 through 255, and 2 bytes could contain values ranging from 0 through 65,535. Referring to Table 3.1, you’ll note that unsigned short is the type that supports this range and comprises 16 bits. Thus, it is quite easy to model positive values in bits and bytes (see Figure 3.1).
How do you model negative numbers in this space? One way is to “sacrifice” a bit as the sign bit that indicates whether the values contained in the other bits are positive or negative (see Figure 3.2). The sign bit needs to be the most-significant-bit (MSB) as the least-significant-bit is required for storing odd numbers. So, when the MSB contains sign information, it is assumed that 0 is positive and 1 means negative, and the other bytes contain the absolute value.
Thus, a signed number that stores 16 bits can contain values ranging from –32,768 through 32,767, and one that stores 8 bits can contain values ranging from –128 through 127. If you look at Table 3.1 again, note that (signed) short is the type that supports positive and negative integer values in a 16-bit space.
Signed Integer Types short, int, long, and long long
Integer types short, int, long, and long long differ in their sizes and the range of values they can contain. int is possibly the most used type and is 32 bits wide on most compilers. It is important to use the right type, based on your projection of the maximum value that a particular variable would be expected to hold.
Declaring a variable of a signed type is simple:
short int gradesInMath = -5; // not your best score int moneyInBank = -70000; // overdraft long populationChange = -85000; // reducing population long long countryGDP_YoY = -70000000000; // GDP lower by 70 billion
Unsigned Integer Types unsigned short, unsigned int, unsigned long, and unsigned long long
Unlike their signed counterparts, unsigned integer variable types have not sacrificed a bit to store sign information, and therefore they support twice as many positive values.
Declaring a variable of an unsigned type is as simple as this:
unsigned short int numColorsInRainbow = 7; unsigned int numEggsInBasket = 24; // will always be positive unsigned long numCarsInNewYork = 700000; unsigned long long countryMedicareExpense = 70000000000;
Avoiding Overflow Errors by Selecting Correct Data Types
Data types such as short, int, long, unsigned short, unsigned int, unsigned long, and the like have a finite capacity for containing numbers. When you exceed the limit imposed by the type chosen in an arithmetic operation, you create an overflow.
Take unsigned short as an example. Data type short consumes 16 bits and can hence contain values from 0 through 65,535. When you add 1 to 65,535 in an unsigned short, the value overflows to 0. It’s like the odometer of a car that suffers a mechanical overflow when it can support only five digits and the car has traveled 99,999 kilometers (or miles).
In this case, unsigned short was never the right type for such a counter. The programmer would have been better off using unsigned int to support numbers higher than 65,535.
In the case of a signed short integer, which has a range of –32,768 through 32,767, adding 1 to 32,767 may result in the signed integer taking the highest negative value. This behavior is compiler dependent.
Listing 3.4 demonstrates the overflow errors that you can inadvertently introduce via arithmetic operations.
Input ▼
Listing 3.4 Examples of the Ill Effects of Signed and Unsigned Integer Overflow Errors
1: #include<iostream> 2: using namespace std; 3: 4: int main() 5: { 6: unsigned short uShortValue = 65535; 7: cout << "unsigned short 65535 + 1 = "; 8: cout << ++uShortValue << endl; 9: 10: short signedShort = 32767; 11: cout << "signed short 32767 + 1 = "; 12: cout << ++signedShort << endl; 13: 14: return 0; 15: }
Output ▼
unsigned short 65535 + 1 = 0 signed short 32767 + 1 = -32768
Analysis ▼
The output indicates that unintentional overflow situations result in unpredictable and unintuitive behavior for the application. Lines 8 and 12 increment an unsigned short and a signed short that have previously been initialized to their maximum supported values, –65,535 and 32,767, respectively. The output demonstrates the values the integers hold after the increment operation—namely an overflow of 65,535 to 0 in the unsigned short and an overflow of 32,767 to –32,768 in the signed short. You wouldn’t expect the result of an increment operation to reduce the value in question, but that is exactly what happens when an integer type overflows. If you were using the values in question to allocate memory, then with the unsigned short, you could reach a point where you would request 0 bytes when your actual need is 65,536 bytes.
Floating-Point Types float and double
Floating-point types are used to store real numbers—that is, numbers that can be positive or negative and that can contain decimal values. So, if you want to store the value of pi (22 / 7, or 3.14) in a variable in C++, you use a floating-point type.
Declaring variables of these types follows exactly the same pattern as the int in Listing 3.1. So, a float that allows you to store decimal values would be declared as
float pi = 3.14;
And a double precision float (called simply a double) is defined as
double morePrecisePi = 22.0 / 7;