在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
采用打印标准头文件中的相应值: #include <stdio.h> #include <limits.h> int main() { printf("采用打印标准头文件limits.h中的相应值:\n"); //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); //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; } 执行结果: 直接计算的方式: #include <stdio.h> //determine ranges of types int main() { printf("采用直接计算的方式:\n"); //signed types printf("signed char min = %d\n", -(char)((unsigned char)~0>>1)-1); printf("signed char max = %d\n", (char)((unsigned char)~0>>1)); 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)); //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; } 执行结果: 备注:
a: (unsigned char)~0用二进制表示为:11111111 b: (unsigned char)~0 >> 1,将(unsigned char)~0逻辑右移1位,结果为:01111111 c: (char)((unsigned char)~0 >> 1),将((unsigned char)~0 >> 1)由无符号类型强制转换为有符号类型 d: -(char)((unsigned char)~0 >> 1),得到的二进制结果为11111111,最高位为符号为,对应的十进制数为:-127
参考资料: [1] http://www.cnblogs.com/wangzhiyu811/archive/2011/07/12/2104377.html [2] http://blog.csdn.net/roma823/article/details/6367142 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论