C++/Data Type/sizeof

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Determining the Size of Variable Types

<source lang="cpp">

  1. include <iostream>

int main() {

  using std::cout;
  cout << "The size of an int is:\t\t" << sizeof(int)    << " bytes.\n";
  cout << "The size of a short int is:\t" << sizeof(short)  << " bytes.\n";
  cout << "The size of a long int is:\t" << sizeof(long)   << " bytes.\n";
  cout << "The size of a char is:\t\t" << sizeof(char)   << " bytes.\n";
  cout << "The size of a float is:\t\t" << sizeof(float)  << " bytes.\n";
  cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
  cout << "The size of a bool is:\t" << sizeof(bool)   << " bytes.\n";
  return 0;

}


 </source>


Get array length with sizeof function

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <iterator>
  4. include <vector>

using namespace std; int main(){

  const int a[] = { 98, 7, 54, 69, 87, 88, 56, 92, 77,39, };
  const int len = sizeof( a ) / sizeof( a[0] );

}


 </source>