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 line #include <iostream> includes the iostream library, which provides input and output functionality in C++.
  2. The line using namespace std; declares that we’re using the std namespace, which contains various standard C++ functions and objects, including cout.
  3. The int main() function is the entry point of the program. It returns an integer value, typically 0, indicating successful execution.
  4. The code uses multiple cout statements to display the sizes of different data types.
  5. Each cout statement follows a similar pattern: it uses the << operator to insert strings and values into the output stream.
  6. 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.
  7. The sizeof operator is also multiplied by 8 to calculate the size in bits.
  8. The output format includes the size in bytes and the corresponding size in bits for each data type.
  9. The endl manipulator is used to insert a newline character into the output stream, separating each line.