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