Data types in C/C++ are used in the declaration of all variables to limit the type of data that can be stored. As a result, data types are used to inform variables about the types of data they can store. When a variable is declared in C++, the compiler allocates memory for it based on the data type that it is declared with. Each sort of data necessitates a particular quantity of memory.
In C++, data types are separated into three categories:
- 1. Type of Primitive Data
- 2. Data Types Derived from Other Data(Derived data type)
- 3. User-Defined Data Types or Abstract Data Types
- Primitive Data Types: These data types are built-in or preset data types that the user can declare variables with directly. For instance, int, char, float, bool, and so on. The following primitive data types are available in C++:
- Integer
- Character
- Floating Point Boolean
- Floating Points in Twos
- Wide Character
- Valueless or Void
2. Derived Data Types: Derived Data Types are data types that are derived from primitive or built-in datatypes.
There are four different varieties of these:
- Function
- Array
- Pointer
- Reference
3. User-Defined Data Types: These are data types that are defined by the user. As an example, in C++, defining a class or a structure. The following user-defined datatypes are available in C++:
- Class
- Structure
- Union
- Enumeration
Defining a typedef DataType
4. Modifiers in Datatypes: Datatype modifiers are used with built-in data types to change the length of data a data type can carry, as the name implies.
- Signed
- Unsigned
- Short
- Long
Data Type | Size (in bytes) | Range |
---|---|---|
short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
unsigned int | 4 | 0 to 4,294,967,295 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 8 | 0 to 4,294,967,295 |
long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
float | 4 | |
double | 8 | |
long double | 12 | |
wchar_t | 2 or 4 | 1 wide character |
// C++ program to sizes of data types #include<iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << " byte" << endl; cout << "Size of int : " << sizeof(int) << " bytes" << endl; cout << "Size of short int : " << sizeof(short int) << " bytes" << endl; cout << "Size of long int : " << sizeof(long int) << " bytes" << endl; cout << "Size of signed long int : " << sizeof(signed long int) << " bytes" << endl; cout << "Size of unsigned long int : " << sizeof(unsigned long int) << " bytes" << endl; cout << "Size of float : " << sizeof(float) << " bytes" <<endl; cout << "Size of double : " << sizeof(double) << " bytes" << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << " bytes" <<endl; return 0; }
Output:
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
You may like:
Features of C++ | C++ Tutorial
Pingback: Operators In C++ Programming - Developers Dome
Pingback: Type Conversion In C++ Programming - Developers Dome
Pingback: if...else - Nested if...else | C++ Programming - Developers Dome