练习2-1:
编写一个程序以确定分别由signed及unsigned限定的char、short、int与long类型变量的取值范围。采用打印标准头文件中的相应值 以及直接计算两种方式实现。后一种方法的实现较困难一些,因为要确定各种浮点类型的取值范围。
方法1:用<limits.h>
头文件中的常量,直接包含头文件就行。
#include <stdio.h>
#include <limits.h>
/* determine range of types*/
int main(void)
{
//signed types
printf("signed char min = %d\n", SCHAR_MIN);
printf("signed char max = %d\n", SCHAR_MAX);
printf("signed short min = %d\n", SHRT_MIN);
printf("signed short max = %d\n", SHRT_MAX);
printf("signed int min = %d\n", INT_MIN);
printf("signed int max = %d\n", INT_MAX);
printf("signed long min = %ld\n", LONG_MIN);
printf("signed long max = %ld\n", LONG_MAX);
printf("*************************************************************\n");
//unsigned types
printf("unsigned char max = %u\n", UCHAR_MAX);
printf("unsigned short max = %u\n", USHRT_MAX);
printf("unsigned int max = %u\n", UINT_MAX);
printf("unsigned long max = %lu\n", ULONG_MAX);
return 0;
}
/*
signed char min = -128
signed char max = 127
signed short min = -32768
signed short max = 32767
signed int min = -2147483648
signed int max = 2147483647
signed long min = -9223372036854775808
signed long max = 9223372036854775807
*************************************************************
unsigned char max = 255
unsigned char max = -1
unsigned short max = 65535
unsigned int max = 4294967295
unsigned long max = 18446744073709551615
*/
方法2:
#include <stdio.h>
int main(void)
{
//signed types
printf("signed char min = %d\n", -(char)((unsigned char) ~0 >> 1) - 1); //-127 - 1
printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1)); //127
printf("signed short min = %d\n", -(short)((unsigned short) ~0 >> 1) - 1);
printf("signed short max = %d\n", (short)((unsigned short) ~0 >> 1));
printf("signed int min = %d\n", -(int)((unsigned int) ~0 >> 1) - 1);
printf("signed int max = %d\n", (int)((unsigned int) ~0 >> 1));
printf("signed long min = %ld\n", -(long)((unsigned long) ~0 >> 1) - 1);
printf("signed long max = %ld\n", (long)((unsigned long) ~0 >> 1));
printf("*************************************************************\n");
//unsigned types
printf("unsigned char max = %u\n", (unsigned char) ~0);
printf("unsigned short max = %u\n", (unsigned short) ~0);
printf("unsigned int max = %u\n", (unsigned int) ~0);
printf("unsigned long max = %lu\n", (unsigned long) ~0);
return 0;
}
/*
signed char min = -128
signed char max = 127
signed short min = -32768
signed short max = 32767
signed int min = -2147483648
signed int max = 2147483647
signed long min = -9223372036854775808
signed long max = 9223372036854775807
*************************************************************
unsigned char max = 255
unsigned char max = -1
unsigned short max = 65535
unsigned int max = 4294967295
unsigned long max = 18446744073709551615
*/
首先,无符号的最大值好求,n位的数,最大值就是 2 n − 1 2^n-1 2n−1,用二进制就是11111……,n个1。0的原码是00000……n个0,将其取反即得到所有位都是1的最大值。
注意:
printf("unsigned char max = %u\n", (unsigned char) ~0);
//unsigned char max = 255
这里打印格式是%u,所以必然输出非负才正确。如果将unsigned char 改成char,取反后就是-1,计算机就会提升为int型,从而输出int型的最大值。
printf("unsigned char max = %u\n", (char) ~0); //与下面的输出-1效果一样
printf("unsigned char max = %u\n", -1);
//unsigned char max = 4294967295
//unsigned char max = 4294967295
再者~
取反符号的位置不要搞错了。
printf("unsigned char max = %d\n", (unsigned char) ~0);
printf("unsigned char max = %d\n", ~(unsigned char) 0);
printf("unsigned char max = %u\n", ~(unsigned char) 0); //必然为非负数,提升为int
printf("unsigned char max = %u\n", -1);
//unsigned char max = 255
//unsigned char max = -1
//unsigned char max = 4294967295
//unsigned char max = 4294967295
所以需要强制转换后再参与格式化输出,以限定它打印时不被自动转换。
然后,输出无符号的范围
printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1)); //127
<<左移运算符,左移运算符会在右边空出的位置填补0。左移n位等价于该数乘 2 n 2^n 2n。
>>右移运算符,在对unsigned char(无符号)值进行右移时会在左边空出的位置填0。如果是有符号(char),有些机器会用1填补左边空出的位置(算术移位),有些机器会用0(逻辑移位)。
对于以二进制补码存放的机器而言,有符数的最最大值的二进制表示为最高位为0,后面全是1,01111……。
对于补码,可以看这篇:补码
- ~0 对0取反,得到所有二进制位全部为1
- (unsigned char) ~0,转化为无符号型,因为右移有符号数结果是不确定的。
printf("signed char max = %d\n", (char)(~0 >> 1)); //-1 0先被提升int,然后我的机器最左边补1
printf("signed char max = %d\n", (char)(~0)); //-1
printf("signed char max = %d\n", (char)((unsigned char) ~0 << 1)); //-2 左移最右边补0
printf("signed char max = %d\n", (char)(~0 << 1)); //-2
-
(unsigned char) ~0 >>1,右移一位最左边空出的一位填0,得到想到的效果。
-
(char)((unsigned char) ~0 >>1),因为是求有符号数,再转回去
有符号数最小值:
M i n ( c h a r ) = − M a x ( c h a r ) − 1 Min(char) = -Max(char) - 1 Min(char)=−Max(char)−1
通过此式有:
printf("signed char min = %d\n", -(char)((unsigned char) ~0 >> 1) - 1); //-127 - 1