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 as important as choosing the right tools for the job! A Phillips screwdriver won’t work well with a regular screw head just like an unsigned integer can’t be used to store values that are negative! Table 3.1 enlists the various variable types and the nature of data 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 |
The following sections explain the important types in greater detail.
Using Type bool to Store Boolean Values
C++ provides a type that is specially created for containing Boolean values true or false, both of which are reserved C++ keywords. This type is particularly useful in 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 alwaysOnTop = false;
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
Use type char to store a single character. A sample declaration is
char userInput = 'Y'; // initialized char to 'Y'
Note that memory is comprised of bits and bytes. Bits can be either 0 or 1, and bytes can contain numeric representation using these bits. So, working or assigning character data as shown in the example, 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. Thus, what the compiler does is store 89 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 the memory in the form of bits and bytes. A memory location that is 1 byte large 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 large can contain a maximum of 2 to the power 8 values—that is, 256 unique values. Similarly, a memory location that is 16 bits large can contain 2 to the power 16 values—that is, 65,536 unique values.
If these values were to be unsigned—assumed to be only positive—then one byte could contain integer values ranging from 0 through 255 and two bytes would contain values ranging from 0 through 65,535, respectively. Look at Table 3.1 and note that the unsigned short is the type that supports this range, as it is contained in 16 bits of memory. Thus, it is quite easy to model positive values in bits and bytes (see Figure 3.1).
FIGURE 3.1 Organization of bits in a 16-bit unsigned short integer.
How to model negative numbers in this space? One way is to “sacrifice” a bit as the sign-bit that would indicate if 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-one would be required to model odd numbers. So, when the MSB contains sign-information, it is assumed that 0 would be positive and 1 would mean negative, and the other bytes contain the absolute value.
FIGURE 3.2 Organization of bits in a 16-bit signed short integer.
Thus, a signed number that occupies 8 bits can contain values ranging from –128 through 127, and one that occupies 16 bits can contain values ranging from –32,768 through 32,767. If you look at Table 3.1 again, note that the (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
These types differ in their sizes and thereby differ in the range of values they can contain. int is possibly the most used type and is 32 bits wide on most compilers. Use the right type depending on your projection of the maximum value that 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 countryGDPChange = -70000000000;
Unsigned Integer Types unsigned short, unsigned int, unsigned long, and unsigned long long
Unlike their signed counterparts, unsigned integer variable types cannot contain sign information, and hence they can actually 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;
Avoid 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 for 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 done 99,999 kilometers (or miles).
In this case, unsigned short was never the right type for such a counter. The programmer was 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.
LISTING 3.4 Demonstrating the Ill-Effects of Signed and Unsigned Integer Overflow Errors
0: #include <iostream> 1: using namespace std; 2: 3: int main() 4: { 5: unsigned short uShortValue = 65535; 6: cout << "Incrementing unsigned short " << uShortValue << " gives: "; 7: cout << ++uShortValue << endl; 8: 9: short signedShort = 32767; 10: cout << "Incrementing signed short " << signedShort << " gives: "; 11: cout << ++signedShort << endl; 12: 13: return 0; 14: }
Output
Incrementing unsigned short 65535 gives: 0 Incrementing signed short 32767 gives: -32768
Analysis
The output indicates that unintentional overflow situations result in unpredictable and unintuitive behavior for the application. Lines 7 and 11 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 value they hold after the increment operation, namely an overflow of 65,535 to zero in the unsigned short and an overflow of 32,767 to –32,768 in the signed short. One 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 can reach a point where you request zero bytes when your actual need is 65536 bytes.
Floating-Point Types float and double
Floating-point numbers are what you might have learned in school as real numbers. These are numbers that can be positive or negative. They 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 would 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 the following:
float pi = 3.14;
And a double precision float (called simply a double) is defined as
double morePrecisePi = 22.0 / 7;