如何计算ANSI C中整数数据类型的范围

如何计算ANSI C中整数数据类型的范围,c,ansi,C,Ansi,ANSI C中计算有符号、无符号、短数据类型和长数据类型范围的数学公式是什么?无符号类型的范围为0到2^类型-1使用的有效位数 签名类型具有实现定义的最小值: 2的补码-2^类型-1使用的有效位数 所有其他-2^类型使用的有效位数-1-1 有符号类型的最大值为2^类型-1-1使用的有效位数 ^是幂函数,不是xor /* Hope it helps * C Program to Print the Range~~ Google */ #include <stdio.h> #defi

ANSI C中计算有符号、无符号、短数据类型和长数据类型范围的数学公式是什么?

无符号类型的范围为0到2^类型-1使用的有效位数

签名类型具有实现定义的最小值:

2的补码-2^类型-1使用的有效位数 所有其他-2^类型使用的有效位数-1-1

有符号类型的最大值为2^类型-1-1使用的有效位数

^是幂函数,不是xor

/* Hope it helps
 * C Program to Print the Range~~ Google
 */
#include <stdio.h>
#define SIZE(x) sizeof(x)*8

void signed_one(int);
void unsigned_one(int);

void main()
{
    printf("\nrange of int");
    signed_one(SIZE(int));
    printf("\nrange of unsigned int");
    unsigned_one(SIZE(unsigned int));
    printf("\nrange of char");
    signed_one(SIZE(char));
    printf("\nrange of unsigned char");
    unsigned_one(SIZE(unsigned char));
    printf("\nrange of short");
    signed_one(SIZE(short));
    printf("\nrange of unsigned short");
    unsigned_one(SIZE(unsigned short));

}
/* RETURNS THE RANGE SIGNED*/
void signed_one(int count)
{
    int min, max, pro;
    pro = 1;
    while (count != 1)
    {
        pro = pro << 1;
        count--;
    }
    min = ~pro;
    min = min + 1;
    max = pro - 1;
    printf("\n%d to %d", min, max);
}
/* RETURNS THE RANGE UNSIGNED */
void unsigned_one(int count)
{
    unsigned int min, max, pro = 1;

    while (count != 0)
    {
        pro = pro << 1;
        count--;
    }
    min = 0;
    max = pro - 1;
    printf("\n%u to %u", min, max);
}
程序说明

将字节数乘以8,将字节数转换为位。 使用两个函数,即signed_one和unsigned_one,分别计算有符号和无符号数据类型的范围。 在步骤1获得的值作为参数发送给两个函数。在这两个函数中,它都是通过变量计数接收的。 将两个函数中的变量pro初始化为1。 在函数中,使用while循环对条件计数进行签名_one!=1,将变量pro向左移动1个位置,并将变量计数连续减少1。 循环终止时,将pro的补码指定给变量min,并将min增加1。减小变量pro,并将其指定给变量max.Print min和max作为输出。 在函数unsigned_one中,使用while循环和条件计数=0,将变量pro向左移动1个位置,并将变量计数连续减少1。 循环终止时,将零指定给变量min。减小变量pro,并将其指定给变量max。Print min和max作为输出。
你说的数学公式是什么意思?它们应该在标准本身中指定,对吗?你认为为什么需要这样做?这些通常存在于极限中。h?@SouravGhosh我的意思是不用程序自己计算范围。我对ANSI不是很确定,但一般来说,你也不需要程序来计算。它们将在相应的标准中定义。@EugeneSh。sizeoftype*字符_BIT@EugeneSh. 是的。在C99§5.2.4.2.1中,在限制中定义。符号类型的范围为0到2^位中的类型大小-1与C规范不一致,因为这些类型可能有填充。然而,这样的平台很少见。@chux你是对的。写入类型使用的有效位数是否正确?我从来没有使用过一个标准类型有位填充的系统。同意-位中类型的大小是符号、值和填充位的总和。注:。。。签名字符不应有任何填充位。。。C11 dr§6.2.6.2 2,因此其他类型可能有。