All variables use data-type during declaration to confine the type of information to be put away. Thusly, we can say that data types are utilized to tell the variables the type of data it can put away. At whatever point a variable is characterized in C++, the compiler assigns some memory for that variable based on the data-type with which it is announced. Each data type requires a different measure of memory.

Data type in c++


Data types in C++ is predominantly divided into three types: 
  1. Primitive Data Type
  2. Derived Data Type
  3. User-Defined Data Type


Primitive Data Types in C++: These data types are implicit or predefined data types and can be utilized straight by the client to pronounce variables. example: int, char , float, bool and so on . Primitive data types accessible in C++ are: 

  • Integer
  • Character
  • Boolean
  • Floating Point
  • Double Floating Point
  • Valueless or Void
  • Wide Character


Derived Data Types in C++: The datatypes that are gotten from the primitive or underlying data types are referred to as Derived Data Types. These can be of four kinds to be specific:

  • Function
  • Array data type
  • Pointer data type
  • Reference data type


Abstract or User-Defined Data Types in C++: These data types are characterized by client or user itself. Like, characterizing a class in C++ or a structure. C++ gives the accompanying user-defined data types: 

  • Class
  • Structure
  • Union
  • Enumeration
  • Typedef defined Data Type


This article discusses primitive data types available in C++. 
 

Integer: Keyword utilized for whole number data types is int. Integers regularly requires 4 bytes of memory space and ranges from -2147483648 to 2147483647. 
 

Character: Character data type is utilized for putting away characters. Keyword utilized for character data type is char. Characters ordinarily requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255. 
 

Boolean Data type: Boolean data type is utilized for putting away boolean or logical values. A boolean factor can store either true or false. Keyword utilized for boolean information type is bool
 

Floating Point: Floating Point information type is utilized for putting away single precision floating point values or decimal worth. Keyword utilized for floating point information type is float. Float variables ordinarily requires 4 byte of memory space. 
 

Double Floating Point: Double Floating Point data type is utilized for putting away double precision floating point values or decimal worth. Keyword utilized for double floating point information type is double. Double variables ordinarily requires 8 byte of memory space. 
 

void: Void means without any value. void datatype addresses a worthless entity. Void data type is utilized for those function which doesn't returns a value. 
 

Wide Character: Wide character data type is also a character information type but this data type has size greater than the normal 8-bit datatype. Addressed by wchar_t. It is generally 2 or 4 bytes long. 
 

 

Datatype Modifiers in C++

As the name suggests, datatype modifiers are utilized with the built-in data types to alter the length of information that a specific data type can hold. 

Data type modifiers accessible in C++ are: 
 

  • Signed data type
  • Unsigned data type
  • Short data type
  • Long data type

Beneath table sums up the adjusted size and scope of built-in datatypes when joined with the type modifiers:


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


#include <iostream>
using namespace std;
int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}


 Output: 

Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes

Previous                                                                          Next