C++ sizeof Operator

Introduction

sizeof operator gets the number of bytes occupied by a type, or by a variable.

Here are some examples of its use:

#include <iostream>

int main()/*w w  w . ja  va2s .co m*/
{
    int height {74};
    std::cout << "The height variable occupies " << sizeof height << " bytes." << std::endl;
    std::cout << "Type \"long long\" occupies " << sizeof (long long) << " bytes." << std::endl;
    std::cout << "The expression height*height/2 occupies "
              << sizeof (height*height/2) << " bytes." << std::endl;
}

Determining Storage Size using operator sizeof() for the int, char, and bool data types.

#include <iostream>
using namespace std;
int main()/*from  w  ww .j  av  a  2 s  . co  m*/
{
   cout << "\nData Type   Bytes"
   << "\n---------   -----"
   << "\nint          " << sizeof(int)
   << "\nchar         " << sizeof(char)
   << "\nbool         " << sizeof(bool)
   << '\n';
   return 0;
}



PreviousNext

Related