C 程序查找数据类型范围

使用 C 语言库和不使用 C 语言库编写一个 C 程序来查找数据类型的范围。在此编程语言中,所有范围信息,例如最小和最大常量,都将包含在 limits 和 float 头文件中。

limits.h 头文件包含有关整数和字符的信息。同时,float.h 文件包含浮点常量。在此 C 示例中,我们使用 limits 头文件并打印 int 和 char 的范围。

#include <stdio.h>
#include<limits.h>

int main()
{   
    printf("The Range of Signed Characters   = %d to %d\n", SCHAR_MIN, SCHAR_MAX);
    
    printf("The Range of Unsigned Characters = 0 to %d\n", UCHAR_MAX);

    printf("The Range of Signed Integer      = %d to %d\n", INT_MIN, INT_MAX);
    
    printf("The Range of Unsigned Integer    = 0 to %d\n", UINT_MAX);

    printf("The Range of Signed Short Int    = %d to %d\n", SHRT_MIN, SHRT_MAX);
    
    printf("The Range of Unsigned Short Int  = 0 to %d\n", USHRT_MAX);

    printf("The Range of Signed Long         = %ld to %ld\n", LONG_MIN, LONG_MAX);
    
    printf("The Range of Unsigned Long       = 0 to %lu\n", ULONG_MAX);

    return 0;
}
C Program to Find the Range of Data Types

在此示例中,我们使用 float.h 头文件查找浮点数据类型的范围。

#include <stdio.h>
#include<float.h>

int main()
{   
    printf("The Float       = %e to %e\n", FLT_MIN, FLT_MAX);

    printf("The Double      = %e to %e\n", DBL_MIN, DBL_MAX);

    printf("The Long Double = %Le to %Le\n", LDBL_MIN, LDBL_MAX);

    return 0;
}
The Float       = 1.175494e-38 to 3.402823e+38
The Double      = 2.225074e-308 to 1.797693e+308
The Long Double = 3.362103e-4932 to 1.189731e+4932

在此程序中,我们将通过创建函数手动查找所有可用数据类型的范围。

#include <stdio.h>

void sigRag(int bytes)
{
    int bits = 8 * bytes;
    long long min = -(1LL << (bits - 1));
    long long max = ((1LL << (bits - 1)) - 1);
    printf("%lld to %llu\n\n", min, max);
}

void unsiRa(int bytes)
{
    int bits = 8 * bytes;
    unsigned long long max = (1LLU << (bits - 1)) + ((1LL << (bits - 1)) - 1);
    printf("0 to %llu\n\n", max);

}
int main()
{   
    printf("The Signed Characters   = ");
    sigRag(sizeof(char)); 
    printf("The Unsigned Characters = ");
    unsiRa(sizeof(unsigned char));

    printf("The Signed Integer      = ");
    sigRag(sizeof(int));
    printf("The Unsigned Integer    = ");
    unsiRa(sizeof(unsigned int));

    printf("The Signed Short Int    = ");
    sigRag(sizeof(short));
    printf("The Unsigned Short Int  = ");
    unsiRa(sizeof(unsigned short));

    printf("The Signed Long         = ");
    sigRag(sizeof(long));
    printf("The Unsigned Long       = ");
    unsiRa(sizeof(unsigned long));

    printf("The Float               = ");
    sigRag(sizeof(float));

    printf("The Double              = " );
    sigRag(sizeof(double));

    printf("The Long Long           = ");
    sigRag(sizeof(long long));

    printf("The Unsigned Long Long  = ");
    unsiRa(sizeof(unsigned long long));
    return 0;
}
The Signed Characters   = -128 to 127

The Unsigned Characters = 0 to 255

The Signed Integer      = -2147483648 to 2147483647

The Unsigned Integer    = 0 to 4294967295

The Signed Short Int    = -32768 to 32767

The Unsigned Short Int  = 0 to 65535

The Signed Long         = -9223372036854775808 to 9223372036854775807

The Unsigned Long       = 0 to 18446744073709551615

The Float               = -2147483648 to 2147483647

The Double              = -9223372036854775808 to 9223372036854775807

The Long Long           = -9223372036854775808 to 9223372036854775807

The Unsigned Long Long  = 0 to 18446744073709551615