我正在尝试解决在 C 中打印基本数据类型范围的问题。
但我看到这些值是错误的。谁能指出我的错误?
#include<stdio.h>
#include<limits.h>
#include<float.h>
main()
{
FILE *fp=fopen("/*path to the file*/","w");
fprintf(fp,"Printing the ranges for data types:\n");
fprintf(fp,"Type Min_Value Max_Value Range\n");
fprintf(fp,"*************************************************************************************************************************************************\n");
fprintf(fp,"char %4d %4d%4d\n\n",CHAR_MIN,CHAR_MAX,CHAR_MAX-CHAR_MIN);
fprintf(fp,"uchar 0 %4d %4d\n\n",UCHAR_MAX,UCHAR_MAX);
fprintf(fp,"schar %d %4d %4d\n\n",SCHAR_MIN,SCHAR_MAX,SCHAR_MAX-SCHAR_MIN);
fprintf(fp,"int %li %li %lu\n\n",(INT_MIN,INT_MAX,INT_MAX-INT_MIN));
fprintf(fp,"uint %d %lu %u\n\n",0,UINT_MAX,UINT_MAX);
fprintf(fp,"long %lli %llu %llu\n\n",LONG_MIN,LONG_MAX,LONG_MAX-LONG_MIN);
fprintf(fp,"ulong %ll %ll %lu\n\n",0,ULONG_MAX,ULONG_MAX);
fprintf(fp,"short %ll %ll %ld\n\n",SHRT_MIN,SHRT_MAX,SHRT_MAX-SHRT_MIN);
fprintf(fp,"ushort %ld %ll %ld\n\n",0,USHRT_MAX,USHRT_MAX);
fclose(fp);
}
这是输出
Printing the ranges for data types:
Type Min_Value Max_Value Range
*************************************************************************************************************************************************
char -128 127 255
uchar 0 255 255
schar -128 127 255
int -1 127 255
uint 0 4294967295 4294967295
long 9223372034707292160 18047113275375615 18047512710020968
ulong 0
short -32768
ushort 0 65535
更新代码并使用格式说明符后,int 和 unsigned int 和 long 的值是错误的,这里是更新。
printf("int %15d %15d %15u\n\n",INT_MIN,INT_MAX,INT_MAX-INT_MIN);
printf("uint %u %15u %15u\n\n",0,UINT_MAX,UINT_MAX);
printf("long %15l %15l %15lu\n\n",LONG_MIN,LONG_MAX,LONG_MAX-LONG_MIN);