2、sizeof.cpp 651 B

123456789101112131415161718192021222324252627
  1. #include <iostream>
  2. int main2()
  3. {
  4. //整形 :short 2bytes int 4bytes long 4bytes long long 8bytes
  5. //可以利用sizeof求出数据类型占用内存大小
  6. //语法: sizeof(数据类型 / 变量)
  7. short num1 = 10;
  8. std::cout << "short占用的内存空间为:" << sizeof(short) << std::endl;
  9. int unm2 = 10;
  10. std::cout << "int占用的内存空间为:" << sizeof(int) << std::endl;
  11. long num3 = 10;
  12. std::cout << "long占用的内存空间为:" << sizeof(long) << std::endl;
  13. long long num4 = 10;
  14. std::cout << "long占用的内存空间为:" << sizeof(long) << std::endl;
  15. //整形大小比较
  16. //short < int <= long <= long long
  17. system("pause");
  18. return 0;
  19. }