Archlinux
GCC 13.1.1 20230429
2023-07-20 18:22:15 星期四
点击查看代码
#include<stdio.h>
#include<limits.h>
int main()
{
// 使用打印标准头文件中的相应值的方式
printf("使用标准头文件中的相应值:\n");
printf("signed char 取值范围: %d 到 %d\n", SCHAR_MIN, SCHAR_MAX);
printf("unsigned char 取值范围: 0 到 %u\n", UCHAR_MAX);
printf("signed short 取值范围: %d 到 %d\n", SHRT_MIN, SHRT_MAX);
printf("unsigned short 取值范围: 0 到 %u\n", USHRT_MAX);
printf("signed int 取值范围: %d 到 %d\n", INT_MIN, INT_MAX);
printf("unsigned int 取值范围: 0 到 %u\n", UINT_MAX);
printf("signed long 取值范围: %ld 到 %ld\n", LONG_MIN, LONG_MAX);
printf("unsigned long 取值范围: 0 到 %lu\n", ULONG_MAX);
// 使用直接计算的方式
printf("\n使用直接计算的方式:\n"); // '~0'的结果是11111111,右移,01111111,char转为有符号类型
printf("signed char 取值范围: %d 到 %d\n", -(char)((unsigned char)~0 >> 1) - 1, (char)((unsigned char)~0 >> 1));
printf("unsigned char 取值范围: 0 到 %u\n", (unsigned char)~0);
printf("signed short 取值范围: %d 到 %d\n", -(short)((unsigned short)~0 >> 1) - 1, (short)((unsigned short)~0 >> 1));
printf("unsigned short 取值范围: 0 到 %u\n", (unsigned short)~0);
printf("signed int 取值范围: %d 到 %d\n", -(int)((unsigned int)~0 >> 1) - 1, (int)((unsigned int)~0 >> 1));
printf("unsigned int 取值范围: 0 到 %u\n", (unsigned int)~0);
printf("signed long 取值范围: %ld 到 %ld\n", -(long)((unsigned long)~0 >> 1) - 1, (long)((unsigned long)~0 >> 1));
printf("unsigned long 取值范围: 0 到 %lu\n", (unsigned long)~0);
return 0;
}
运行截图:
该段代码来自gpt3.5 ,在观察limits.h和float.h后该角简单,由gpt代写,输出正确。
小白刚学习C语言,代码质量不高,欢迎评论。
标签:short,int,unsigned,long,char,printf,取值 From: https://www.cnblogs.com/yuwu/p/17569341.html