As mentioned earlier, a variable can only store data of a specific type. This means the value assigned to a variable must match its data type. But what exactly is a type? A type defines what kind of data a variable can hold. For example:
int cubes;
Here, cubes is the variable name, the semicolon (;) marks the end of the statement, and int is the data type of the variable.
Below is a table that provides information about the fundamental C++ data types.
| Data Type | Minimum Value | Maximum Value | Memory (Bytes) |
|---|---|---|---|
char | -128 | 127 | 1 |
short | -32,768 | 32,767 | 2 |
int | -2147483648 | 2147483647 | 4 (or 2 in older compilers) |
long | -9223372036854775808 | 9223372036854775807 | 8 |
float | 1.2 × 10⁻³⁸ | 3.4 × 10³⁸ | 4 |
double | 2.2 × 10⁻³⁰⁸ | 1.8 × 10³⁰⁸ | 8 |
long double | Varies by compiler (e.g., 80-bit precision on x86 architectures) | Varies by compiler (e.g., 80-bit precision on x86 architectures) | 8, 10, 12, or 16 |
These data types allow you to store both positive (+) and negative (-) values. Therefore, they are called signed data types. However, if you want your variables to store only positive values, you can use Unsigned Data Types, which also modify the range of values they can hold by effectively doubling the upper limit of their corresponding signed types. For example, unsigned short can store values from 0 to 65,535, whereas a signed short can store -32,768 to 32,767.”
Below is a table for Unsigned Data Types.
| Data Type | Minimum Value | Maximum Value | Memory (Bytes) |
|---|---|---|---|
unsigned char | 0 | 255 | 1 |
unsigned short | 0 | 65,535 | 2 |
unsigned int | 0 | 4,294,967,295 | 4 |
unsigned long | 0 | 18446744073709551615 | 8 |
With these data types, you can assign the desired value to a variable.
Note: Sizes and ranges of data types are defined by the C++ standard (e.g., C++11, C++14, etc.) and may vary slightly across different implementations.
Let us examine each data type separately.
An integer is a whole number, such as 11, -7, etc. Now, let’s look at the different integer data types available in C++.
short (identical to short int)int long (identical to long int)They are all integer data types; just range of minimum and maximum value and sizes are different, as mentioned above. Whenever you need to store an integer value in memory, you can use any of these data types.
Below are examples of how to declare variables using these integer types:
short small_number;
int number;
long int large_number;
You can also write them as given below. It will compile without an error. But it is not recommended. Because in this case, int is simply redundant.
short int small_number;
int number;
long int large_number;
In C++, the char data type is used to store characters. Each character value must be enclosed in single quotes ('A'). This distinguishes it from strings and ensures it occupies 1 byte of memory.
In C++, char is an integer type because it stores the ASCII value of a character in memory. This means that a char variable can store both characters (e.g., 'A') and their corresponding ASCII values (e.g., 65 for 'A'). For example:
char d = 65;
char b = 'A';
Here:
char d = 65; stores the numeric value to memory as it is. And when you print this char, compiler displays it as A.char b = 'A'; The compiler converts it into its ASCII equivalent value (65) and stores this ASCII value in 1 byte of memory. And when you print this char, compiler displays it as A.#includeusing namespace std; int main() { char variable = 'Z'; char a = 41; cout << "Output is:" << a << endl << "and " << variable; return 0; }
variable stores 'Z'.a stores 41, which is interpreted as an ASCII code.41 corresponds to ) in the ASCII table.41 along with the character 'Z'.Output is:)
and Z
This example demonstrates how char variables can hold both characters and numeric ASCII values.
Some values cannot be stored in integer types, such as numbers containing decimal points (e.g., 3.14, 11.561). These values require floating-point data types, such as:
floatdoubleThe difference between float and double is that float uses less memory but has lower precision, while double provides higher precision at the cost of more memory.
Let’s see an example:
#includeusing namespace std; int main() { // Floating point variables float a = 3.14; double d = 5.69; double result; // Performing multiplication result = a * d; cout << "Answer is: " << result; return 0; }
Answer is: 17.8666
Note: You cannot use short or long as in the code below with a char or float. Because short and long are different data types from char and float data types and cannot be combined like this.
short char var = 'A'; //Will cause error:
short float = 0.6;
But you can combine long with double. And you cannot combine short with double.
long double = 0.6; //Will compile without an error
short double = 0.6; //Will cause error
No Comments