C++ Example - Check the Size of int, float, double, and char Variables
Use the C++ sizeof operator to calculate the space occupied by int, float, double, and char variables.
sizeof operator syntax:
sizeof(dataType);
Note: The results may vary across different systems.
Example
#include <iostream>
using namespace std;
int main()
{
cout << "char: " << sizeof(char) << " bytes" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "float: " << sizeof(float) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
return 0;
}
The output of the above program is:
char: 1 byte
int: 4 bytes
float: 4 bytes
double: 8 bytes