Data Types in C++

When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.

The memory in our computers is organized in bytes.

A byte is the minimum amount of memory that we can manage in C++.

A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255).

In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.

Below you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:

Name Description Size* Range*
int Integer 4 bytes -2,147,483,648 to 2,147,483,647
char Character 1 byte -128 to 127 or 0 to 255
float Floating point 4 bytes 1.2E-38 to 3.4E+38
double Double precision floating point 8 bytes 2.3E-308 to 1.7E+308
bool Boolean 1 byte true or false
short Short integer 2 bytes -32,768 to 32,767
long Long integer 4 bytes (or 8 bytes on some systems) -2,147,483,648 to 2,147,483,647 (or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
long long Long long integer 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned int Unsigned integer 4 bytes 0 to 4,294,967,295
unsigned char Unsigned character 1 byte 0 to 255
unsigned short Unsigned short integer 2 bytes 0 to 65,535
unsigned long Unsigned long integer 4 bytes (or 8 bytes on some systems) 0 to 4,294,967,295 (or 0 to 18,446,744,073,709,551,615)
unsigned long long Unsigned long long integer 8 bytes 0 to 18,446,744,073,709,551,615

The values of the columns Size and Rangedepend on the system the program is compiled for. The values shown above are those found on most 32-bit systems.

But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always 1 byte in size.

The same applies to the floating point types float, double and long double , where each one must provide at least as much precision as the preceding one.

To know the size of a data type or a variable on your system, you can use the sizeof operator. The sizeof operator is used to get the size of a variable or data type. The syntax of the sizeof operator is as follows:

sizeof(data_type)

The sizeof operator returns the size of the data type in bytes. For example, the following code will return the size of an integer in bytes:

int main(){
cout<<sizeof(int)<<endl;
return 0;
}

Now that you have learned the different types of data type and how to identify one in C++ programming. Lets learn about how to Declare Variables in C++ in the next section.