# Program to Calculate the Size of Each Data Type [C, C++, Python & Java]

Posted in  Vinay Khatri
Last updated on October 5, 2023

This tutorial will help you write a program in C, C++, Python, and Java to display a program to calculate the size of each data type. It is very straightforward to write this program. You simply need to know the language syntax and data types.

## What is a Data Type?

A data type refers to the type of value a variable holds and the type of mathematical, logical, or relational operations the value supports. For instance, the data type ``` int ``` (integer) holds the whole numbers, ``` str ``` (string) holds characters, and ``` float ``` holds decimal points.

Data types are a must in every programming language. Each data type has a specific size. This means when you declare a variable using a specific data type, it consumes the same amount of memory as the size of that data type. For example, the ``` int ``` data type is of size 4 bytes. Hence, when you declare a variable of the data type ``` int ``` , the variable consumes 4 bytes of memory.

## C Program Calculate the Size of Each Data Type

``````#include<stdio.h>
int main()
{
printf("The size of short integer is: %2d", sizeof(short int));
printf("\nThe size of int is: %2d", sizeof(int));
printf("\nThe size of int * is: %2d", sizeof(int *));
printf("\nThe size of long int is: %2d", sizeof(long int));
printf("\nThe size of lonf int * is: %2d", sizeof(long int *));
printf("\nThe size of signed int is: %2d", sizeof(signed int));
printf("\nThe size of unsigned int is: %2d", sizeof(unsigned int));
printf("\n");
printf("\nThe size of float is: %2d", sizeof(float));
printf("\nThe size of float * is: %2d", sizeof(float *));
printf("\n");
printf("\nThe size of double is: %2d", sizeof(double));
printf("\nThe size of double * is: %2d", sizeof(double *));
printf("\nThe size of long double is: %2d", sizeof(long double));
printf("\n");
printf("\nThe size of char is: %2d", sizeof(char));
printf("\nThe size of char * is: %2d", sizeof(char *));
printf("\nThe size of signed char is: %2d", sizeof(signed char));
printf("\nThe size of unsigned char is: %2d", sizeof(unsigned char));
return 0;
}``````

Output:

``````The size of short integer is:  2
The size of int is:  4
The size of int * is:  8
The size of long int is:  8
The size of lonf int * is:  8
The size of signed int is:  4
The size of unsigned int is:  4

The size of float is:  4
The size of float * is:  8

The size of double is:  8
The size of double * is:  8
The size of long double is: 16

The size of char is:  1
The size of char * is:  8
The size of signed char is:  1
The size of unsigned char is:  1``````

## C++ Program Calculate the Size of Each Data Type

``````#include<iostream>
using namespace std;
int main()
{
cout<<"The size of short integer is: "<<sizeof(short int);
cout<<"\nThe size of int is: "<<sizeof(int);
cout<<"\nThe size of int * is: "<<sizeof(int *);
cout<<"\nThe size of long int is: "<<sizeof(long int);
cout<<"\nThe size of lonf int * is: "<<sizeof(long int *);
cout<<"\nThe size of signed int is: "<<sizeof(signed int);
cout<<"\nThe size of unsigned int is: "<<sizeof(unsigned int);
cout<<"\n";
cout<<"\nThe size of float is: "<<sizeof(float);
cout<<"\nThe size of float * is: "<<sizeof(float *);
cout<<"\n";
cout<<"\nThe size of double is: "<<sizeof(double);
cout<<"\nThe size of double * is: "<<sizeof(double *);
cout<<"\nThe size of long double is: "<<sizeof(long double);
cout<<"\n";
cout<<"\nThe size of char is: "<<sizeof(char);
cout<<"\nThe size of char * is: "<<sizeof(char *);
cout<<"\nThe size of signed char is: "<<sizeof(signed char);
cout<<"\nThe size of unsigned char is: "<<sizeof(unsigned char);
return 0;
}``````

Output:

``````The size of short integer is: 2
The size of int is: 4
The size of int * is: 8
The size of long int is: 8
The size of lonf int * is: 8
The size of signed int is: 4
The size of unsigned int is: 4

The size of float is: 4
The size of float * is: 8

The size of double is: 8
The size of double * is: 8
The size of long double is: 16

The size of char is: 1
The size of char * is: 8
The size of signed char is: 1
The size of unsigned char is: 1``````

## Python Program Calculate the Size of Each Data Type

``````import sys

print(f"Size of int: {sys.getsizeof(int())} bytes")
print(f"Size of float: {sys.getsizeof(float())} bytes")
print(f"Size of bool: {sys.getsizeof(bool())} bytes")
print(f"Size of str: {sys.getsizeof(str())} bytes")
print(f"Size of bytes: {sys.getsizeof(bytes())} bytes")
print(f"Size of list: {sys.getsizeof(list())} bytes")
print(f"Size of tuple: {sys.getsizeof(tuple())} bytes")
print(f"Size of set: {sys.getsizeof(set())} bytes")
print(f"Size of dict: {sys.getsizeof(dict())} bytes")``````

Output

``````Size of int: 24 bytes
Size of float: 24 bytes
Size of bool: 24 bytes
Size of str: 49 bytes
Size of bytes: 33 bytes
Size of list: 56 bytes
Size of tuple: 40 bytes
Size of set: 216 bytes
Size of dict: 64 bytes``````

## Java Program Calculate the Size of Each Data Type

``````import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println("Size of int: " + (Integer.SIZE / 8) + " bytes.");
System.out.println("Size of short: " + (Short.SIZE / 8) + " bytes.");
System.out.println("Size of long: " + (Long.SIZE / 8) + " bytes.");
System.out.println("Size of char: " + (Character.SIZE / 8) + " bytes.");
System.out.println("Size of float: " + (Float.SIZE / 8) + " bytes.");
System.out.println("Size of double: " + (Double.SIZE / 8) + " bytes.");
}
}``````

Output:

``````Size of int: 4 bytes.
Size of short: 2 bytes.
Size of long: 8 bytes.
Size of char: 2 bytes.
Size of float: 4 bytes.
Size of double: 8 bytes.``````

## Conclusion

We hope you found it easy to write a program to calculate the size of different data types in C, C++, Python, and Java. If you don't know the size of any data type, you can easily find it using the above program.