关闭

[C++]cpp小笔记3 --- C++基础类型的大小和转换

标签: C++
506人阅读 评论(0) 收藏 举报
分类:

1. 类型的大小

  • A short integer is at least 16 bits wide.
  • An int integer is at least as big as short.
  • A long integer is at least 32 bits wide and at least as big as int.
  • A long long integer is at least 64 bits wide and at least as big as long.

2. sizeof and climits

int days = 0
cout << "size of days is " << sizeof(days) << endl;
cout << "size of int is " << sizeof(int) << endl;

Results:
size of days is 4
size of int is 4

climits中定义了c++中基础类型的最大值,最小值,大小的常量值。
CHAR_BIT          Number of bits in a char
CHAR_MAX       Maximum char value
CHAR_MIN        Minimum char value
SCHAR_MAX    Maximum signed char value
SCHAR_MIN     Minimum signed char value
UCHAR_MAX   Maximum unsigned char value
SHRT_MAX       Maximum short value
SHRT_MIN        Minimum short value
USHRT_MAX    Maximum unsigned short value

climits的使用
#include <climits>
// use limits.h for older systems


3.  无符号类型

unsigned short change;
unsigned int rovert;
unsigned quarterback;  //unsigned int type
unsigned long gone;
unsigned long long lang_lang;



从这张图上可以看出
If signed integer max value is 32767. 32767 + 1, result is -32768. 
If unsigned integer max value is 65535, if 65535 + 1, result is 0.

4. 类型转换。

自动类型转换:
  • C++会自动将converts bool, char, unsigned char, signed char, and short values to int.
  • The unsigned short type is converted to int if short is smaller than int. If the two types are the same size, unsigned short is con-
    verted to unsigned int. 
    This rule ensures that there’s no data loss in promoting unsigned short.
  • When an operation involves two types, the smaller is converted to the larger.
强制转换:

int a = 0;
double b = (double)a; 

OR 

double b = double(a);

double b = static_cast<double> (a);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:265947次
    • 积分:4321
    • 等级:
    • 排名:第7471名
    • 原创:71篇
    • 转载:229篇
    • 译文:4篇
    • 评论:39条
    最新评论