Integer data types hold whole numbers. C++ has eight different integer data types. They differ based on how large of an integer they can store and whether they can store both positive and negative integers, or only nonnegative integers. The size or range of the data that can be stored in an integer data type is determined by how many bytes are allocated for storage. Because a bit can hold 2 values, 0 or 1, you can calculate the number of possible
values by calculating 2n where n is the number of bits. Given 8 bits per byte, a short integer which is allocated 2 bytes can store 216 (65,536) possible 0 and 1 combinations. If we split those between negative and positive values, the data range for a short is -32,768 to +32,767. Data Type Size* Range short 2 bytes -32,768 to +32,767 unsigned short 2 bytes 0 to +65,535 int 4 bytes -2,147,483,648 to +2,147,483,647 unsigned int 4 bytes 0 to +4,294,967,295 long 4 bytes -2,147,483,648 to +2,147,483,647 unsigned long 4 bytes 0 to +4,294,967,295 long long 8 bytes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 unsigned long long 8 bytes 0 to +8,446,744,073,709,551,615 *Do you notice something strange about the size numbers in this table? The int and long data types are the same size so they have the same data ranges. The C++ language specification only requires the following: In some systems, longs will be allocated more bytes than ints. The numbers in the table are for a program compiled by Visual Studio 2013 running on
Microsoft Windows 7. You can determine the memory allocated for a data type or a variable by using the sizeof operator.Integer data types
Checking memory allocation using the sizeof operator
Creating integer variables
As previously discussed, the variable declaration is comprised of the type followed by the variable name followed by a semicolon. Here are a few examples.
unsigned long myVariable1; short myVariable2; unsigned int myVariable3;You can declare multiple variables in a single statement. Just enter the data type and follow that with a comma separated list of variable names. End with a semicolon. The declaration statement can span multiple lines and you can assign values to the variables too.
int var1, var2, var3, var4; // creating 4 int variables short sVar1, // variable declaration can span multiple lines sVar2, sVar3; long lVar1 = 10, lVar2 = 20, lVar3 = 30; // creating and assigning 3 long variablesInteger constants/literals
Numeric literals without decimal points are treated by the compiler as int values. An integer literal is also referred to as an integer constant.
If you need the compiler to treat an integer constant as a long, unsigned int, or unsigned long, you can add the following suffixes to the literal value.
u or U (case does not matter) | unsigned | 5u (unsigned int) |
l or L | long | 5L |
u/U and l/L combinations | unsigned long | 5ul, 5lu (order does not matter) |
Floating point data types
Floating point data types are used to store real numbers (numbers that can have a fractional part). There are three C++ floating point data types, float, double, and long double.
float | 4 bytes | ±3.4E-38 and ±3.4E38 | 7 |
double | 8 bytes | ±1.7E-308 and ±1.7E308 | 16 |
long double | 8 bytes | ±1.7E-308 and ±1.7E308 | 16 |
Like the integer types, double and long double have the same size in some systems but in others, long doubles are of greater size.
All floating point data types store both positive and negative numbers.
The double data type is often referred to as double precision as it is twice the size of the float type and has more significant digits. Most if not all higher level mathematical operators will use double floating point data types.
Floating point constants/literals
Numeric literals in decimal notation or E (exponent) notation are treated as double values. Floating point literals are often called floating point constants.
Examples of floating point constants:
10.1
1.5E5
1.5e5
1.5e+5
2.63E-6
2.63e-6
Note that you can use e or E and you can include or exclude the + for positive exponents.
To treat a floating point constant as float or long double, use the following suffixes.
f or F (case does not matter) | float | 10.1f |
l or L | long double | 10.1L |
Many compilers will issue a warning if you assign a floating point constant to a float variable since you are assigning a double to a float so the number may be truncated.
To prevent this, add an f suffix to the literal.
float myFloatVar; myFloatVar = 10.1f; // add f suffix to define float literalAssigning floating point values to integer variables
When you assign a floating point value (literal or variable) to an integer variable, the decimal part of the number is discarded.
int demoInt; demoInt = 1.234; // assign a floating point constant to an int variable cout << "value of demoInt: " << demoInt << endl; float demoFloat = 4.567; demoInt = demoFloat; // assign a float to an int variable cout << "value of demoInt after assigning demoFloat: " << demoInt << endl;The char data type
The char data type is used for single characters. A char literal is a single character enclosed in single quotes.
char demoChar = 'a'; // 'a' is a char literalA char variable normally uses 1 byte of memory.
The C++ string class
A string is sequence of characters. C++ does not include a built in string data type. However, the standard library defines a string class allowing programmers to create string variables.
Using the string class
You must include the string header file.
#include <string>
String literals
String literals are enclosed in double quotes. String literals can include spaces and escape sequences.
A simple string example
#include <iostream> #include <string> // we want to work with strings so we include this header // from standard library using namespace std; int main() { string myName = "Tim"; // string literals are enclosed in double quotes cout << "My name is: " << myName << endl; return 0; }
The bool data type
The data type bool is for Boolean variables. Boolean data are either true or false.
bool myBooleanVariable = false; // initialize as false myBooleanVariable = true; // switch value to trueThe assignment statements use the words true or false and not the strings “true” and “false”.
Bool data are stored in memory as int values. False is stored as 0 and true is stored as 1.
bool myBooleanVariable = false; // initialize as false cout << "output bool defined as false: " << myBooleanVariable << endl; myBooleanVariable = true; // switch value to true cout << "output bool defined as true: " << myBooleanVariable << endl;Wrapping up variables
In our discussion of variables and data types, we have shown a lot of examples of variable initialization.
Variable initialization uses the assignment operator, =. The data that operators work on are called operands. The assignment operator requires two operands.
The left operand must be a location in memory whose contents can be changed such as a variable. C++ refers to this as an lvalue.
The right side operand is called an rvalue and it must be an expression that has a value.
You will sometimes see references to l-value or lvalue in compiler error messages. When you see this, your left hand operand does not refer to a memory location whose contents can be changed.
1 = 2; // 1 does not reference a changeable memory location so it is not an lvalueThe 1 on the left is a numeric constant whose value cannot be changed.