sizeof

The sizeof operator is a compile-time operator that returns the size, in bytes, of the argument passed to it. For example, the following code uses sizeof to display the sizes of a number of variables:

  
  struct EmployeeRecord {
    int ID;
    int age;
    double salary;
    EmployeeRecord* boss;
  };

  ...

  cout << "sizeof(int): " << sizeof(int) << endl
       << "sizeof(float): " << sizeof(float) << endl
       << "sizeof(double): " << sizeof(double) << endl
       << "sizeof(char): " << sizeof(char) << endl
       << "sizeof(EmployeeRecord): " << sizeof(EmployeeRecord) << endl;

  int i;
  float f;
  double d;
  char c;
  EmployeeRecord er;

  cout << "sizeof(i): " << sizeof(i) << endl
       << "sizeof(f): " << sizeof(f) << endl
       << "sizeof(d): " << sizeof(d) << endl
       << "sizeof(c): " << sizeof(c) << endl
       << "sizeof(er): " << sizeof(er) << endl;

When run, the above code displays this output:

  
  sizeof(int): 4
  sizeof(float): 4
  sizeof(double): 8
  sizeof(char): 1
  sizeof(EmployeeRecord): 20
  sizeof(i): 4
  sizeof(f): 4
  sizeof(d): 8
  sizeof(c): 1
  sizeof(er): 20

Note that sizeof can either take a variable type (such as int) or a variable name (such as i in the example above).

It is also important to note that the sizes of various types of variables can change depending on what system you're on. Check out a description of the C and C++ data types for more information.