C++ Program to Find of Size of Datatypes

In the previous article, we have discussed about C++ Program to Display Factors of a Number. Let us learn how to find Size of Datatypes in C++ Program.

Method to find the size of datatypes in c++

In this article, we will discuss how we can find the size of data types in c++. The method that we will discuss today is given below.

Let’s first discuss what a datatype is. All variables use data-type during declaration to restrict the type of data to be stored. For example, if we want a variable should store an integer so we gave this variable datatype int. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory. This memory requirement is known as the size of the datatype. Let’s discuss the method of how we can get the size of datatype in c++.

Method 1-Using sizeof operator

Sizeof the operator in c++ is used to calculate the size of the variable in c++. When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data type.



Let’s discuss it with the help of examples.

#include <iostream>
using namespace std;

int main() {
    int integerType; 
    char charType; 
    float floatType; 
    double doubleType; 
    cout << "Size of int is: " << 
    sizeof(integerType) <<"\n"; 
    cout << "Size of char is: " << 
    sizeof(charType) <<"\n"; 
    cout << "Size of float is: " << 
    sizeof(floatType) <<"\n";
    cout << "Size of double is: " << 
    sizeof(doubleType) <<"\n"; 
    return 0;


Size of int is: 4
Size of char is: 1
Size of float is: 4
Size of double is: 8

Note: sizeof() may give different output according to the machine.