Latest Code Tutorials

C++ Data Type Example | Data Types in C++ Tutorial

C++ data type is an inbuilt keyword that defines the type of a variable. For example, in C++ if we want to declare an integer type data type, then we have to write int number; However, there are three types of Data Type in C++.

All variables use the data-type during declaration to restrict a specific type of data to be stored. Therefore, we can say that the data types are used to tell the variables a particular type of data it can store.

Whenever the variable is defined in C++, the compiler allocates some memory for that variable based on a data type with which it is declared. Every data type requires a different amount of memory.

C++ Data Type

  1. Primary(Built-in) Data Types:
    • character
    • integer
    • floating point
    • boolean
    • double floating-point
    • void
    • wide character
  2. User-Defined Data Types:
    • Structure
    • Union
    • Class
    • Enumeration
  3. Derived Data Types:
    • Array
    • Function
    • Pointer
    • Reference

See the following figure.

C++ Data Types

Now, we will learn about these three types of data types in detail.

#Built-in Data Type

char Data Type

The keyword used to declare character type data type is char. That data type is used to hold a single character in the variable. The size of each character is 2 bytes. 


char ch=’a’;

int Data Type

The data type used to declare integer data type is int. The above data type can store values between -2,147,483,648 to 2,147,483,647. That means -231 to 231-1. 

Size: 4 bytes.


int x=21;

float Data Type

When we have to store the values in decimal, then we can have the use of the float data type. Float is the 32-bit precision IEEE 754 floating-point. The float data type can store values from 3.4e−038 to 3.4e+038. 

Size: 4 bytes.


float x=5.4;

double Data Type:  

When we have to store huge decimal digits, we should go with a double data type. It can store up to 15 decimal digits, fractional numbers from 1.7e−308 to 1.7e+038.

Size: 64bits means 8 bytes.


double x = 54.9d

boolean Data Type:

When we have two values, either True or False, we can use the boolean keyword. That data type holds two values, either it is true or false.

These values are mostly used in conditional cases, like if the condition is satisfied return true otherwise return false. 


bool b=false;

void Data Type:

This data type does not hold any value. It is used in those functions which don’t have anything to return. 


void myfunction(parameter)

Wide character:

This data type is also a character type data type, but it has a size of greater than the standard 8bit data type. For example, if you run code on Microsoft’s compiler, this will have 16bits, but in Linux, the same data type will have size 32 bit.

It is always advised not to use this data type. However, this data type is declared with a wchar_t keyword.

So, see the following table.

Type Keyword
Character char
Boolean bool
Floating point float
Integer int
Double floating point double
Valueless void
Wide character wchar_t

#Program of built-in data type in C++

using namespace std;

//declaring void data type and it has no return type
void fun(int i, float f)
    cout<<"\n\nThe value of int declare in main is: "<<i;
    cout<<"\nThe value of float declare in main is: "<<f<<"\n";
int main()
    char ch='D';
    cout<<"The value of char is :"<<ch;
    int x=456561;
    cout<<"\nThe value of int is :"<<x;
    float f=65.321;
    cout<<"\nThe value of float is :"<<f;
    double d=54.6451;
    cout<<"\nThe value of double is :"<<d;
    bool b=false;
    //here it will print 0
    //in C++ 0 means false in case of boolean data type
    cout<<"\nThe value of boolean is :"<<b;
    //calling the void type function

    return 0;

See the following output.


Data Types in C++ Example

#Data Type Modifier

It is used to modify the size of the built-in data type in C++. There are mainly four types of data type modifier available in C++; those are:


This data type is used when we have a large integer, and there is a chance of overflow by using int keyword. The size of this data type is 4 byte.

Range: -2,147,483,648 to 2,147,483,647


We have long for a large integer, but what if we have a very small integer. In that case, declaring int data type will take more space, that’s why there is the data type short.  

Size: 2 Byte

Range: -32,768 to 32,767


The signed data type means when value can be negative or positive. By default, all data types are signed.


The unsigned data types are used when we know that the value can only be a positive one. In this case, the range starts from 0.

For example, some range of unsigned value is:

unsigned int: 0 to 4,294,967,295

unsigned long int: 0 to 4,294,967,295

unsigned char: 0 to 255

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 4 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

#User defined Data Types

User-defined data types mean which can be declared according to our need using the keywords. We will understand it below:

#Structure Data Type in C++

The structure is a collection of different types of the variable under a single name. It is quite similar to the class concept. It consists of some structure members and one object name.

See the following syntax.

struct type_name 
   member_type1 member_name1;
   member_type2 member_name2;
} object_names;

See the following example.

struct student
	int roll;
	string name;

#Union Data Type in C++

The union is also similar to structure, but the difference between structure and union is, in structure the size of the structure is the total sum of all variables, but in the union, the size of the union is the size of the largest variable.

See the following syntax.

union type_name 
  member_type1 member_name1;
  member_type2 member_name2;
} object_names;

See the following code example.

union student
     int roll;
     string name;

#Enum Data Type in C++

Enum (Enumeration) is a user-defined datatype where we specify the set of values for a variable, and the variable can only take one out of a small set of possible values. It consists of an integral constant.


enum enum_name {const1, const2, const3,..., const-n};

See the following example.

enum months {jan, feb, march, april, may};

Here, enum name is month and Jan, Feb, March… are the type of months.

#Derived data types in C++


An array is a collection of similar data types whose values are stored in a contiguous memory location. For example, if we have to store values 23,43,12 and 3, then we can use the array to store later. For this, if the starting address in the memory location of the array is 2840, then the next index will be 2844 (because int size is 4 byte) and the ending index will be 2852.


data_type array_name[array_size];

Example: int arr[5];


A function is a group of statement or code which performs some specific tasks. There is always a default function in c++, and that is the main().

Functions are mainly two types:

  1. System Defined: Which are already present in the system.
  2. User-Defined: Which needs to be declared by the user; this can perform tasks according to user requirement.

#Pointer Data Type in C++

The pointer is a kind of variable that stores the address of another variable.


<type> * pointerName;   <type> can be an int, float, char, etc.

Example: int *ptr;

#typedef Declarations

You can create a new name for an existing type using a typedef. Following is the simple syntax to define a new type using a typedef.

typedef type newname; 

For example, the following tells the compiler that feet are another name for int.

typedef int feet;

Now, the following declaration is perfectly legal and creates an integer variable called distance.

feet distance;

So here we end the concepts of data types of C++.

Finally, C++ Data Type Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.