关于带符号与无符号类型:整型 int、short 和 long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。
一字节表示八位,即:1byte = 8 bit;
《C和指针》中写过:long与int:标准只规定long不小于int的长度,int不小于short的长度。
类型名称 字节数 取值范围
signed char 1 -2^7 ~ 2^7-1 -128~+127
short int 2 -2^14 ~ 2^14-1 -32768~+32767
int 4 -2^31 ~ 2^31-1 -2147483648~+2147483647
unsigned int 4 0 ~ 2^32-1 0 ~ 4294967295
long int 4 -2^31 ~ 2^31-1 -2147483648~+2141483647 (同int)
unsigned long 4 0 ~ 2^32-1 0~4294967295
long long int 8 -2^63 ~ 2^63-1 -9223372036854775808~+9223372036854775807
unsigned long long 8 0 ~ 2^64-1 18446744073709551615
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
特别需要注意的是:int类型与long类型表示的的数据范围是一样的,所以当int的表示范围不能做到时,用long也并不能解决问题,这时候需要用long long类型。
PS:表示取值范围的上限或者下限时可以使用左移运算符(<<)实现。比如:
int的取值范围可表示为:-(1 << 31) ~ ((1 << 31) - 1)
左移一位相当于乘2;左移运算符的优先级比加和减的优先级低!!