Datatype size in C++
Tags: cpp, programming
Published: 2020-06-13 01:32:52

Hello friends, We all work with data types and variable while writing a code in any programming  language. But to use any data type for the variable without knowing much about the memory implication is not good.

Though we don't care much about the memory and its implication these days. As we have computers with large memory. But still we should be careful if we are dealing with large number of data types in our program.

In this article I will note down all the datatype and their memory requirement with code examples in C++. Our decision to use any variable depends on the maximum number of value a variable can hold and the bytes it use to store the value in  memory.

#Datatype and its size

Below are some of the most commonly used datatype and their size in bytes. 

The commented code contains value of the variable.

Unsigned Integer

```C++
    int numBytes = 0;
    unsigned int myUnsignedInt = UINT_MAX;

    numBytes = sizeof(myUnsignedInt);
    cout << "Maximum value: " << myUnsignedInt << endl; //4294967295
    cout <<"Bytes used: " <<    numBytes << endl; //4
```

Unsigned long Integer

In case if the above value is not enough for you and you suspect that your variable may contain even bigger value. You can use unsigned long integer.

As we can see in the below code snippet, this type of variable can store large number but it comes up with memory trade of as it uses 8 bytes of memory.

```C++
    unsigned long int myUnsignedLongInt = ULONG_MAX;
    numBytes = sizeof(myUnsignedLongInt);
    cout << "Maximum value: " << myUnsignedLongInt << endl; // 18446744073709551615
    cout <<"Bytes used: " <<  numBytes << endl;//8
```

Unsigned long long Integer

For some compiles Unsigned integer and unsigned long integer can store the same number of maximum value. For those this is an alternate option. 

For my compile the unsigned long and unsigned long long have same value.

```C++
    unsigned long long int myUnsignedLongLongInt = ULONG_LONG_MAX;
    numBytes = sizeof(myUnsignedLongLongInt);
    cout << "Maximum value: " << myUnsignedLongLongInt << endl; // 18446744073709551615
    cout <<"Bytes used: " <<  numBytes << endl;//8
```
Unsigned short int

If you know your variable may contain very less number, in that case it is always better to use this datatype. As we can see there would not be any memory trade of as it need only 2 bytes.

```C++
 unsigned short int myUnsignedShortInt = USHRT_MAX;
    numBytes = sizeof(myUnsignedShortInt);
    cout << "Maximum value: " << myUnsignedShortInt << endl; // 4197360
    cout <<"Bytes used: " <<  numBytes << endl;//2
```
Unsigned char

If your requirement is to use numbers up to 255 and be very efficient you can use unsigned char as shown in the below code. It uses only 1 byte of memory space to store the numbers up to 255.

Though it is primarily used to store characters but we can use to store integers as well.

```C++
    unsigned char myUnsignedByte = UCHAR_MAX;
    numBytes = sizeof(myUnsignedByte);
    cout << "Maximum value: " << (int)myUnsignedByte << endl; // 255
    cout <<"Bytes used: " <<  myUnsignedByte << endl;//1
```

#Conclusion:

If we are working on memory intensive application in that case we should be aware of the different type of variable type and use them accordingly.