C语言怎么计算数据类型范围?
之前在网上看到的一个讨论,是谁决定了数据类型的范围?
比如说,怎么确定 char
就是 -128~127
,而不是 -127~128
呢?
说下规定
signed
的取值范围是 -(2N-1)
to 2N-1 - 1
unsigned
的取值范围是 0
to (2N-1) + (2N-1 - 1)
只要记住一点就好了,就是最小值,有符号的最小值是 -128 开始,因为0占用了一个位置,所以最大值就只能是127。
无符号的最小值是 0 ,那最大值就是255了。
#怎么计算呢?
如果我们知道 #include
这个头文件后,可能就很容易了,因为最大值最小值都在这里面有宏定义。
#看一个老外的代码
#include
#include
#include
int main()
{
printf("Range of signed char %d to %d\n", SCHAR_MIN, SCHAR_MAX);
printf("Range of unsigned char 0 to %d\n\n", UCHAR_MAX);
printf("Range of signed short int %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("Range of unsigned short int 0 to %d\n\n", USHRT_MAX);
printf("Range of signed int %d to %d\n", INT_MIN, INT_MAX);
printf("Range of unsigned int 0 to %lu\n\n", UINT_MAX);
printf("Range of signed long int %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("Range of unsigned long int 0 to %lu\n\n", ULONG_MAX);
// In some compilers LLONG_MIN, LLONG_MAX
printf("Range of signed long long int %lld to %lld\n", LONG_LONG_MIN, LONG_LONG_MAX);
// In some compilers ULLONG_MAX
printf("Range of unsigned long long int 0 to %llu\n\n", ULONG_LONG_MAX);
printf("Range of float %e to %e\n", FLT_MIN, FLT_MAX);
printf("Range of double %e to %e\n", DBL_MIN, DBL_MAX);
printf("Range of long double %e to %e\n", LDBL_MIN, LDBL_MAX);
return 0;
}
#输出
Range of signed char -128 to 127
Range of unsigned char 0 to 255
Range of signed short int -32768 to 32767
Range of unsigned short int 0 to 65535
Range of signed int -2147483648 to 2147483647
Range of unsigned int 0 to 4294967295
Range of signed long int -2147483648 to 2147483647
Range of unsigned long int 0 to 4294967295
Range of signed long long int -9223372036854775808 to 9223372036854775807
Range of unsigned long long int 0 to 18446744073709551615
Range of float 1.175494e-038 to 3.402823e+038
Range of double 2.225074e-308 to 1.797693e+308
Range of long double -0.000000e+000 to -1.#QNAN0e+000
--------------------------------
#尴尬了
要是我记不住那个头文件,或者是我记不住那些宏,那要怎么搞呢?所以就有了下面的代码
#include "string.h"
#include "stdio.h"
int main()
{
printf("Range signed char %lld to %lld\n",-(1LL << (8*sizeof(char) -1)),(1LL << (8*sizeof(char) -1)) - 1);
printf("Range unsigned char 0 to %lld\n",(1LL << (8*sizeof(char) -1)) + (1LL << (8*sizeof(char) -1)) - 1);
printf("Range signed int %lld to %lld\n",-(1LL << (8*sizeof(int) -1)),(1LL << (8*sizeof(int) -1)) - 1);
printf("Range unsigned int 0 to %lld\n",(1LL << (8*sizeof(int) -1)) + (1LL << (8*sizeof(int) -1)) - 1);
printf("Range signed long %lld to %lld\n",-(1LL << (8*sizeof(long) -1)),(1LL << (8*sizeof(long) -1)) - 1);
printf("Range unsigned long 0 to %lld\n",(1LL << (8*sizeof(long) -1)) + (1LL << (8*sizeof(long) -1)) - 1);
printf("Range signed long long %lld to %lld\n",-(1LL << (8*sizeof(long long) -1)),(1LL << (8*sizeof(long long) -1)) - 1);
printf("Range unsigned long long 0 to %llu\n",(1LL << (8*sizeof(long long) -1)) + (1LL << (8*sizeof(long long) -1)) - 1);
}
#输出
Range signed char -128 to 127
Range unsigned char 0 to 255
Range signed int -2147483648 to 2147483647
Range unsigned int 0 to 4294967295
Range signed long -2147483648 to 2147483647
Range unsigned long 0 to 4294967295
Range signed long long -9223372036854775808 to 9223372036854775807
Range unsigned long long 0 to 18446744073709551615
--------------------------------
Process exited after 0.03036 seconds with return value 51
如果对上面的不是很理解的话,可以看看我这个图片
#还有一种写法
这里使用了库函数pow,但是这个函数返回值是double,这里需要特别注意一下。
#include "stdio.h"
#include "math.h"
int main()
{
printf("signed char %ld to %ld\n",(int)-pow(2,sizeof(char)*8-1),(int)pow(2,sizeof(char)*8-1) -1);
printf("unsigned char 0 to %ld\n",(int)pow(2,sizeof(char)*8) -1);
printf("signed int %lld to %lld\n",(long long)-pow(2,sizeof(int)*8-1),(long long)pow(2,sizeof(int)*8-1) -1);
printf("unsigned int 0 to %lld\n",(long long)pow(2,sizeof(int)*8) - 1);
return 0;
}
#输出
signed char -128 to 127
unsigned char 0 to 255
signed int -2147483648 to 2147483647
unsigned int 0 to 4294967295
--------------------------------
因为pow的返回值是 double
使用的时候需要特别注意
# 说一下
数值在计算机里面都是用补码的形式存储的,补码和原码是不一样的,特别是负数。
正数的原码、反码、补码都相同。
原码: 最高位是符号位,其余为是数值位「8421定律」,最高位是0
表示正数,1
表示负数。
负数的反码: 在原码的基础上,符号位不变,数值位取反。
负数的补码: 在反码的基础上加 1 。
评论