C++ Data Types

C++ Data Types: In C++ Data types are nothing but they used to store information in the computer memory with different data types. When a variable declares it is necessary to define a variable with a data type. We can store the information with different data types like integer, floating point, character, double, boolean, etc. Based on the data type of variable the operating system allocates memory and decides what should be stored in the reserved memory.

C++ Data Types

There are different data types present in the C++ language. And are listed as follows:

  • Primitive (or) Built-in data type  (int, char, float, double)
  • Derived data type (array, pointer, etc;)
  • Enumeration data type (enum)
  • User-defined data type (structure)

Primitive (or) Built-in data types (int, char, float, double)

The primitive (or) built-in data types are an integer and floating point based. And C++ supports both signed and unsigned literals.  While the memory size of the data type can be changed according to 32 (or) 64-bit operating system.



Type keyword
Boolean bool
Character char
Integer int
floating point float
Double floating point double
Valueless void
wide character wchar_t

 Primitive data types available in C++

  • Integer
  • Character
  • Boolean
  • Floating point
  • Double Floating point
  • wide character

Built-in data types in C++

These values may vary from compiler to compiler. while these are based on 64 bit.

Data Types Memory Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
short 2 bytes -32,768 to 32,767
signed short 2 bytes -32768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
signed int 2 bytes -32,767 to 32,767
unsigned int 4 bytes 0 to 4,294,967,295
short int 2 bytes -32,7678 to 32,767
signed short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 32,767
long int 4 bytes -2147483648 to 2147483647
signed long int 4 bytes -2147483648 to 2147483647
unsigned long int 4 bytes 0 to 4294967295
long long int 8 bytes -(2^63) to (2^63)-1
unsigned long long int 8 bytes 0 to 18,446,744,073,709,551,615
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
float 4 bytes +/3.4e+/-38(~7 digits)
double 8 bytes +/-1.7e +/-308(~15 digits)
long double 8 bytes +/-1.7e +/-308 (~15 digits)
wchar_t 2 or 4 1 wide character

We can display the size of all the data types by using the sizeof( ) function. And also passing the keyword to function of the data type as an argument.

Example:

#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 char: 1 byte
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

enum data type

It was a user-defined data type having a set of a finite number of enumeration constants. And the keyword ‘enum’ is used to create an “enumerated data type“.

syntax: enum enum-name {list of names}var-list;

Example:

enum cse(software, internet, seo);

Typedef

This was used for creating a new data type. But in common it was used for exchanging data type with another name.

Syntax: typedef [data_type] synonym;

(or)

typedef [data_type] new_data_type;

Example:

typedef int integer;
integer rollno;

User Defined data type (structure)

This data type can be defined by the user as per their requirement. And the structure is a user-defined data type that can be used to group items of possible of different types into a single type.

Syntax:

struct address {
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};

Example:

#include <iostream> 
using namespace std;   
struct Point { 
    int x, y; 
};   
int main() 
{ 
// Create an array of structures 
   struct Point arr[10]; 
//Access array members 
    arr[0].x = 10; 
    arr[0].y = 20; 
   cout << arr[0].x << ", " << arr[0].y; 
    return 0; 
}

Output:

10, 20