Here in this tutorial, we are going to discuss all the various Data Types we use in C++. We will also discuss why we use data types.
Data Figures
C++ is a statically typed programming language so when we initialize a variable or identifier we have to specify its data types along with it. A data type represents the type of the variable which is supposed to hold. When we create a variable it reserved some space in the memory and different types of data occupy different memory sizes. Suppose if you want to store a number to an identifier, for example, x=3, before we do this we have to specify that x would only be able to hold integer values. On the basis of the data type, the operating system allocates memory to the identifier or variable.
C++ Data Types Types
C++ has divided its Data Types into 3 Categories:
- Primary or Primitive
- Derived
- User-Defined
1. Primitive Data Types
Primitive data types are also known as Built-in data types, and these data types define the nature of the variable and determine the size occupied by the variable in the main memory. These are called primitive data types because these data types cannot be divided further. Here are all primitive data types present in C++:
Type | Keyword |
Boolean | bool |
Character | char |
Integer | int |
Floating numbers | float |
Double Floating numbers | Double |
Void | Void |
Wide Character | wchar_t |
- Boolean (bool): This data type is used to hold the logical data, true or false, and to represent it in programming language we use the bool keyword.
- Character (char): As its name suggests character data type is used to store character it could either be Alphabet (capital or small), digit or any special character.
- Integer (int): Integer data type is used to store integer values.
- Floating Numbers (float): Float data type is used to store decimal numbers.
- Double Floating Numbers (double): It is similar to floating data types but it provides a more precise decimal number as compared to floating numbers.
- Void (void): Void data type represents the valueless entity.
- Wide Character (wchar_t): It is similar to character data type, but it occupies more space than char.
Data Type modifiers
Data Type Modifiers are the special keywords used as a prefix to data types, to modify the range and length of the data types. In C++ we have 4 Data Types Modifiers
- Signed
- Unsigned
- Short
- Long
Data Type Modifiers | Data Types: |
Signed | Integer, Char, |
Unsigned | Integer, char |
Long | Integer Double |
Short | Integer |
C++ Data-types Range and Size table
Type | Size | Range |
char | 1byte | -127 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -127 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 4bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 8bytes | same as long int |
unsigned long int | 4bytes | 0 to 4,294,967,295 |
long long int | 8bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | |
double | 8bytes | |
long double | 12bytes | |
wchar_t | 2 or 4 bytes | 1 wide character |
Example:
#include <iostream> using namespace std; int main() { cout <<"DATA TYPES----->SIZE\n"; cout << "Character: " << sizeof(char) <<"byte\n"; cout << " Integer: " << sizeof(int) <<"bytes\n"; cout << "Long Integer: " << sizeof(long int) <<"bytes\n"; cout << "Short int: " << sizeof(short int) <<"bytes\n"; cout << "Float : " << sizeof(float) <<"bytes\n"; cout << "Double: " << sizeof(double) <<"bytes\n"; cout << " Wide Character: " << sizeof(wchar_t) <<"bytes\n"; return 0; }
Output:
DATA TYPES----->SIZE Character: 1byte Integer: 4bytes Long Integer: 4bytes Short int: 2bytes Float : 4bytes Double: 8bytes Wide Character: 2bytes
2. Derived Data Types
Derived Data types are the collection of primitive data types; we can further divide a derived data type into single primitive data types. In C++ we have four types of Derived data types:
- Function
- Array
- Pointers
- Reference
3. Abstract Data Types
These Data Types also known as user-defined data types, with the help of abstract data types we can create our own data types.
- Class
- Structure
- Union
- Typedef
- Enumeration
Summary
- Data types tell the nature of the variable.
- Data types restrict the type of data a variable could hold
- There are three types of Data types in C++ Primitive, Derived, and Abstract Data type.
- Primitive data types are also known as inbuilt data types which include, int, char, float, double, etc.
- Data Modifies is the pre-fix keyword of primitive data types that are used to modify the default size and range of primitive data types.
- Signed, unsigned, short and long are the four data types modifiers present in C++.
- Derived data types are the collection of Primitive data types.
- Abstract data types are also known as User-defined data types.
People are also reading: