In this tutorial, you will learn the basic data types such as int, float, char, etc in the C++ programming language.
In C++ you declare a variable to use in your program. But depending on the type of variable you declare, you can only store a specific type of data in it. Also if you are declaring integer variables, then you cannot store floating-point numbers in them. So you need to know about what type of data you will be working with and accordingly declare your variables.
Then you need to be aware of the type of data you will be working with and then choose the correct range for it to avoid memory wastage. You can go about using long and double for every other program you write to avoid any range errors but that is considered as a bad coding standard. You should always strive for memory and time efficiency in your code.
There are 3 main types of Data types with their division in C++ are:
- Primitive Data Types
- Floating Point
- Double Floating Point
- Valueless or Void
- Wide Character
- Derived Data Types
- Abstract or User-Defined Data Types
- Typedef defined Datatype
We will explore the primitive data types more in this article and later we will explore the remaining Data types.
Primitive Data Types
There are 7 different types of predefined data types in C++ which are as follows:
- Integer – It is used to store only integer values in it. The keyword which is used to declare an integer is
int. Generally, an Integer has a size of 4 bytes which lets it store values from -2147483648 to 2147483647.
- Character – It is used to store only character value in it. The keyword used here is char. The size of one character variable is generally 1 byte of memory which ranges from -128 to 127.
- Boolean – It is used to store logical or Boolean values. A Boolean variable can either store TRUE or FALSE. The keyword which is used to declare a Boolean variable is a bool.
- Floating Point – It is used to store single-precision floating-point values and the keyword used to declare a floating-point variable is
float. The size of a float variable is generally 4 bytes.
- Double Floating Point – It is used to store double-precision floating-point values which are more accurate in value than floating-point values. The keyword used to declare such values is double. A double variable will usually take 8 bytes of space.
- Void – It is a valueless type of entity and declaring a variable as void means that it does not have a specific type. The keyword used to declare void functions or variables is void. It is mostly used for functions that do not return any kind of value and they take up 2 bytes of space in a 16-bit system, 4 bytes in a 32-bit system, and 8 bytes in a 64-bit system.
- Wide Character – It is a type of character data type which has a size greater than 1 byte and is generally 2 or 4 bytes long. The keyword used to declare a wide character is wchar_t.
Note: The size of a data type varies from system to system and hence you can find the exact size on your machine for any data type using this function
sizeof(void); in a 64-bit machine would return 8 as the result which means 8 bytes.
Data Type Modifiers and Value Ranges
Along with the Data types, there are 4 data type modifiers which are given below:
In unsigned variables, the negative part is not considered and the range gets doubled in the positive axis. It is used when you are sure that the values which will come are positive only. A value is signed by default and one needs to mention it as unsigned to use the properties of an unsigned variable.
You can find the range of a variable from their size in bytes by using the formula:
Range of Signed variable: -1*28*No of Bytes in Data type to + 28*No of Bytes in Data type -1
Range of Unsigned variable: 0 to 2 (8*No of Bytes in Data Type) +1 -1
In a table format it can be represented as:
|Data Type||Size(Bytes)||Range of Values|
|int / signed int||4||-2,147,483,648 to 2,147,483,647|
|unsigned int||4||0 to 4,294,967,295|
|char / signed char||1||-128 to 127|
|unsigned char||1||0 to 255|
|short int / signed short int||2||-32,768 to 32,767|
|unsigned short int||2||0 to 65,535|
|long int / signed long int||8||-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|unsigned long int||8||0 to 18,446,744,073,709,551,615|
|float||4||3.4E +/- 38 (7 digits)|
|double||8||1.7E +/- 308 (15 digits)|
|wchar_t||2||0 to 65,535|
|bool||1||True or False|