C++ Program to Find Size of Int, Float, Char and double data types using sizeof operator

Here is a C++ program to find the size of variables in run time using size of operator.


C++ program to find size of variable using sizeof operator

In this program, we will use sizeof operator to find the size of variable at run-time. The size of variable is system dependent. Hence, the output of below program may differ depending upon the system configurations.

sizeof Operator
The sizeof is a compile time operator not a standard library function. The sizeof is a unary operator which returns the size of passed variable or data type in bytes.
As we know, that size of basic data types in C++ is system dependent, So we can use sizeof operator to dynamically determine the size of variable at run time.
#include <iostream>

using namespace std;

int main() {
    // Printing size of Basic Data Types
    cout << "Size of a Character (char) = " << sizeof(char) << " 
        bytes" << endl;
    cout << "Size of an Integer (int) = " << sizeof(int) << " 
        bytes" << endl;
    cout << "Size of a Floating Point (float) = " << sizeof(float) << " 
        bytes" << endl;
    cout << "Size of Double (double) = " << sizeof(double) << " 
        bytes" << endl;

    return 0;
}
Output
Size of a Character Variable (char) = 1 bytes
Size of an Integer Variable (int) = 4 bytes
Size of a Floating Point Variable (float) = 4 bytes
Size of Double Variable (double) = 8 bytes

Recommended Posts
C++ Program to Perform Addition Subtraction Multiplication Division
C++ Program to Print Hello World
C++ Taking Input From User
C++ Program to Multiply two Numbers
C++ Program to Check Whether a Character is Alphabet or Not
C++ Program to Check Whether a Character is Vowel or Not
C++ Program to Find Sum of Natural Numbers
C++ Program to Find Largest of Three Numbers
C++ Program to Make a Simple Calculator
C++ Program to Check Whether Number is Even or Odd
C++ Program to Swap Two Numbers
All C++ Programs