Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) sizeof

 

sizeof is a compile-time operator to determine the size of a data type in bytes.

 

#include <iostream>

int main()
{
  std::cout
    << "bool size: " << sizeof(bool) << '\n'
    << "int size : " << sizeof(int) << '\n';
}

 

 

 

 

 

sizeof being a compile-time operator

 

Because sizeof is a compile-time operator, one can use on it:

 

 

int main()
{
  static_assert(sizeof("")     == 1,"The sizeof of a string is its number of characters + 1");
  static_assert(sizeof("1")    == 2,"The sizeof of a string is its number of characters + 1");
  static_assert(sizeof("12")   == 3,"The sizeof of a string is its number of characters + 1");
  static_assert(sizeof("123")  == 4,"The sizeof of a string is its number of characters + 1");
  static_assert(sizeof("1234") == 5,"The sizeof of a string is its number of characters + 1");
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict