Variables are required to store information when programming in a programming language. A variable retains the memory location of the value it stores. This means that when you create a variable, you leave some space in memory.
You may need to store information for various data types (such as character, wide character, integer, floating point, double floating point, Boolean, and so on), and the operating system allocates memory and decides what to store in reserved memory based on the data type of the variable.
Basic built-in types
C++ provides programmers with a wide range of built-in and user-defined data types. The following table lists seven basic C++ data types:
type | Keyword |
---|---|
Boolean | bool |
Character | char |
integer | int |
float | float |
Double floating point type | double |
No Type | void |
Wide Character Type | wchar_t |
Actually wchar_t Here comes:
typedef short int wchar_t;
So wchar_t actually has the same space as short int.
Some basic types can be modified with one or more type modifiers:
- signed
- unsigned
- short
- long
The following table shows the memory required for each variable type to store values in memory, as well as the maximum and minimum values that a variable of that type can store.
Note: Different systems will vary, with 8 bits per byte.
Note: By default, int, short, long are signed.
Note: long ints are 8 bytes, ints are 4 bytes, and the earlier C compiler defined long ints to take 4 bytes and ints to take 2 bytes. The new C/C++ standard is compatible with the earlier setting.
type | position | Range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 4 bytes | -2147483648 to 2147483647 |
unsigned int | 4 bytes | 0 to 429 4967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | 2 bytes | 0 to 65,535 |
signed short int | 2 bytes | -32768 to 32767 |
long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
signed long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8 bytes | 0 to 18,446,744,073,709,551,615 |
float | 4 bytes | Precision type takes up 4 bytes (32 bits) of memory, +/- 3.4e +/- 38 (~7 digits) |
double | 8 bytes | Double takes up 8 bytes (64 bits) of memory, +/-1.7e +/-308 (~15 digits) |
long double | 16 bytes | Long double precision 16 bytes (128 bits) of memory space, providing 18-19 bits of significant digits. |
wchar_t | 2 or 4 bytes | 1 wide character |
Note that various types of storage sizes are related to the number of bits in the system, but 64-bit systems are the predominant ones currently in use.
The following lists the differences in storage size between 32-bit and 64-bit systems (same as windows):
As you can see from the table above, variables vary in size depending on the compiler and the computer you are using.
The following example outputs the sizes of various data types on your computer.
Example
#include<iostream> #include <limits> using namespace std; int main() { cout << "type: \t\t" << "************size**************"<< endl; cout << "bool: \t\t" << "Bytes occupied:" << sizeof(bool); cout << "\t Maximum:" << (numeric_limits<bool>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "Bytes occupied:" << sizeof(char); cout << "\t Maximum:" << (numeric_limits<char>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "Bytes occupied:" << sizeof(signed char); cout << "\t Maximum:" << (numeric_limits<signed char>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "Bytes occupied:" << sizeof(unsigned char); cout << "\t Maximum:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "Bytes occupied:" << sizeof(wchar_t); cout << "\t Maximum:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "Bytes occupied:" << sizeof(short); cout << "\t Maximum:" << (numeric_limits<short>::max)(); cout << "\t\t Minimum value:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "Bytes occupied:" << sizeof(int); cout << "\t Maximum:" << (numeric_limits<int>::max)(); cout << "\t Minimum value:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "Bytes occupied:" << sizeof(unsigned); cout << "\t Maximum:" << (numeric_limits<unsigned>::max)(); cout << "\t Minimum value:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "Bytes occupied:" << sizeof(long); cout << "\t Maximum:" << (numeric_limits<long>::max)(); cout << "\t Minimum value:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: \t" << "Bytes occupied:" << sizeof(unsigned long); cout << "\t Maximum:" << (numeric_limits<unsigned long>::max)(); cout << "\t Minimum value:" << (numeric_limits<unsigned long>::min)() << endl; cout << "double: \t" << "Bytes occupied:" << sizeof(double); cout << "\t Maximum:" << (numeric_limits<double>::max)(); cout << "\t Minimum value:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "Bytes occupied:" << sizeof(long double); cout << "\t Maximum:" << (numeric_limits<long double>::max)(); cout << "\t Minimum value:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "Bytes occupied:" << sizeof(float); cout << "\t Maximum:" << (numeric_limits<float>::max)(); cout << "\t Minimum value:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "Bytes occupied:" << sizeof(size_t); cout << "\t Maximum:" << (numeric_limits<size_t>::max)(); cout << "\t Minimum value:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "Bytes occupied:" << sizeof(string) << endl; // <<'\t Max:'< < (numeric_limits<string>:: max) () < <'t min:' < < (numeric_limits<string>::min) () < < endl; cout << "type: \t\t" << "************size**************"<< endl; return 0; }
This example uses endl, which inserts a line break after each line, <* The operator is used to pass multiple values to the screen, sizeof() The function is used to get the sizes of various data types.
When the above code is compiled and executed, it produces the following results, which vary depending on the computer used:
type: ************size************** bool: Bytes occupied: 1 Maximum: 1 Minimum value: 0 char: Bytes occupied: 1 Maximum: Minimum value:? signed char: Bytes occupied: 1 Maximum: Minimum value:? unsigned char: Bytes occupied: 1 Maximum:? Minimum value: wchar_t: Bytes occupied: 4 Maximum: 2147483647 Minimum value:-2147483648 short: Bytes occupied: 2 Maximum: 32767 Minimum value:-32768 int: Bytes occupied: 4 Maximum: 2147483647 Minimum value:-2147483648 unsigned: Bytes occupied: 4 Maximum: 4294967295 Minimum value: 0 long: Bytes occupied: 8 Maximum: 9223372036854775807 Minimum value:-9223372036854775808 unsigned long: Bytes occupied: 8 Maximum: 18446744073709551615 Minimum value: 0 double: Bytes occupied: 8 Maximum: 1.79769e+308 Minimum: 2.22507e-308 long double: Bytes occupied: 16 Maximum: 1.18973e+4932 Minimum: 3.3621e-4932 float: Bytes occupied: 4 Maximum: 3.40282e+38 Minimum value: 1.17549e-38 size_t: Bytes occupied: 8 Maximum: 18446744073709551615 Minimum value: 0 string: Bytes occupied: 24 type: ************size**************