This code demonstrates the usage of the sizeof
operator to determine the size of various data types in bytes and bits.
#include <iostream>
using namespace std;
int main () {
cout << "size of bool:\t\t" << sizeof(bool) << " Byte / " << sizeof(bool) * 8 << " Bit" << endl;
cout << "size of char:\t\t" << sizeof(char) << " Byte / " << sizeof(char) * 8 << " Bit" << endl;
cout << "size of integer:\t" << sizeof(int) << " Byte / " << sizeof(int) * 8 << " Bit" << endl;
cout << "size of unsigned integer:" << sizeof(unsigned int) << " Byte / " << sizeof(unsigned int) * 8 << " Bit" << endl;
cout << "size of long integer:\t" << sizeof(long int) << " Byte / " << sizeof(long int)*8 << " Bit" << endl;
cout << "size of float:\t\t" << sizeof(float) << " Byte / " << sizeof(float) * 8 << " Bit" << endl;
cout << "size of double:\t\t" << sizeof(double) << " Byte / " << sizeof(double) * 8 << " Bit" << endl;
cout << "size of long long:\t" << sizeof(long long) << " Byte / " << sizeof(long long) * 8 << " Bit" << endl;
}
size of bool: 1 Byte / 8 Bit
size of char: 1 Byte / 8 Bit
size of integer: 4 Byte / 32 Bit
size of unsigned integer:4 Byte / 32 Bit
size of long integer: 8 Byte / 64 Bit
size of float: 4 Byte / 32 Bit
size of double: 8 Byte / 64 Bit
size of long long: 8 Byte / 64 Bit
cout
statements to display the sizes of different data types.cout
statement follows a similar pattern: it uses the <<
operator to insert strings and values into the output stream.sizeof
operator is used to determine the size of each data type in bytes. It returns the size of the data type in bytes as an unsigned integer.sizeof
operator is also multiplied by 8 to calculate the size in bits.endl
manipulator is used to insert a newline character into the output stream, separating each line.