Read out memory of Data Type

This code demonstrates the usage of the sizeof operator to determine the size of various data types in bytes and bits.

Code Example

#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;
}
Output
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

Code Explanation

  1. The code uses multiple cout statements to display the sizes of different data types.
  2. Each cout statement follows a similar pattern: it uses the << operator to insert strings and values into the output stream.
  3. The 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.
  4. The sizeof operator is also multiplied by 8 to calculate the size in bits.
  5. The output format includes the size in bytes and the corresponding size in bits for each data type.
  6. The endl manipulator is used to insert a newline character into the output stream, separating each line.