简体   繁体   English

C 编程中的长整数范围

[英]long int range in C programming

As per the link https://www.geeksforgeeks.org/data-types-in-c/2根据链接https://www.geeksforgeeks.org/data-types-in-c/2

If we assume long int takes 8 bytes (64 bits) then its range should be -2^63 to 2^63-1, but that is not given in the link above.如果我们假设long int需要 8 个字节(64 位),那么它的范围应该是 -2^63 到 2^63-1,但是上面的链接中没有给出。 Why is it so?为什么会这样?

And similarly unsigned long int should have range between 0 to 2^64.同样unsigned long int的范围应该在 0 到 2^64 之间。

Please tell what will be the range for float , double and long double as it's not mentioned in link.请告知floatdoublelong double的范围,因为链接中没有提到。

I would rather not use geeksforgeeks as the source of knowledge.我宁愿不使用 geeksforgeeks 作为知识的来源。 There are more "imprecise" information on this page此页面上有更多“不精确”的信息

C standard defines minimum ranges of the integer types C 标准定义了 integer 类型的最小范围

— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // −(2 7 − 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 2 7 − 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2 8 − 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2 16 − 1
— minimum value for an object of type int
INT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type int
INT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 2 16 − 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // −(2 31 − 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2 31 − 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2 32 − 1
22 Environment §5.2.4.2.1
WG14/N1256 Committee Draft — Septermber 7, 2007 ISO/IEC 9899:TC3
— minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // −(2 63 − 1)
— maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 2 63 − 1
— maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 2 64 − 1

So the integer is not minimum 32 bits but 16 bits.所以 integer 不是最低 32 位而是 16 位。 The actual size can be checked in the limits.h defines (this comes from the C standard):实际大小可以在limits.h定义中查看(这个来自C标准):

#define CHAR_BIT 8
#define CHAR_MAX UCHAR_MAX or SCHAR_MAX
#define CHAR_MIN 0 or SCHAR_MIN
#define INT_MAX +32767
#define INT_MIN -32767
#define LONG_MAX +2147483647
#define LONG_MIN -2147483647
#define LLONG_MAX +9223372036854775807
#define LLONG_MIN -9223372036854775807
#define MB_LEN_MAX 1
#define SCHAR_MAX +127
#define SCHAR_MIN -127
#define SHRT_MAX +32767
#define SHRT_MIN -32767
#define UCHAR_MAX 255
#define USHRT_MAX 65535
#define UINT_MAX 65535
#define ULONG_MAX 4294967295
#define ULLONG_MAX 18446744073709551615

C standard also sets the minimum range of the floating point types - but you need to check your .h file for the implementation values. C 标准还设置了浮点类型的最小范围 - 但您需要检查.h文件以获取实现值。

#define DBL_DIG 10
#define DBL_MANT_DIG
#define DBL_MAX_10_EXP +37
#define DBL_MAX_EXP
#define DBL_MIN_10_EXP -37
#define DBL_MIN_EXP
#define DECIMAL_DIG 10
#define FLT_DIG 6
#define FLT_MANT_DIG
#define FLT_MAX_10_EXP +37
#define FLT_MAX_EXP
#define FLT_MIN_10_EXP -37
#define FLT_MIN_EXP
#define FLT_RADIX 2
#define LDBL_DIG 10
#define LDBL_MANT_DIG
#define LDBL_MAX_10_EXP +37
#define LDBL_MAX_EXP
#define LDBL_MIN_10_EXP -37
#define LDBL_MIN_EXP

The values given in the following list shall be replaced by implementation-defined constant expressions with values that are greater than or equal to those shown:以下列表中给出的值应替换为实现定义的常量表达式,其值大于或等于所示值:

#define DBL_MAX 1E+37
#define FLT_MAX 1E+37
#define LDBL_MAX 1E+37

The values given in the following list shall be replaced by implementation-defined constant expressions with (positive) values that are less than or equal to those shown:以下列表中给出的值应替换为实现定义的常量表达式,其(正)值小于或等于所示值:

#define DBL_EPSILON 1E-9
#define DBL_MIN 1E-37
#define FLT_EPSILON 1E-5
#define FLT_MIN 1E-37
#define LDBL_EPSILON 1E-9
#define LDBL_MIN 1E-37

If we assume long int takes 8 bytes (64 bits) then its range should be -2^63 to 2^63-1 , but that is not given in the link above.如果我们假设long int需要 8 个字节(64 位),那么它的范围应该是-2^632^63-1 ,但是上面的链接中没有给出。 Why is it so?为什么会这样?

Because GeeksForGeeks is a known source of incorrectness and falseness.因为GeeksForGeeks是不正确和错误的已知来源。 Don't trust in them.不要相信他们。 Trust in official standards and established authors with decades of experience (as you can find here on Stack Overflow - me excluded:-)).相信官方标准和拥有数十年经验的知名作者(您可以在 Stack Overflow 上找到 - 我除外 :-))。

The Definitive C Book Guide and List权威的 C 书籍指南和列表

Under this list you can find books of higher quality.在此列表下,您可以找到质量更高的书籍。

Beside that long int has commonly on the most implementations the same size as an int , 4 byte.除此之外,在大多数实现中, long int通常具有与int相同的大小,4 字节。

But as you said it is an assumption, it is completely correct to ask for.但正如你所说,这是一个假设,要求是完全正确的。

if we assume long int takes 8 bytes(64 bits) then its range should be -2^63 to 2^63-1, but that is not given in the link above.如果我们假设 long int 需要 8 个字节(64 位),那么它的范围应该是 -2^63 到 2^63-1,但是上面的链接中没有给出。 why is it so?为什么会这样?

Because C is specified in a way that allows for fully conforming implementations to be created for platforms that do not follow either of your assumptions -- that long int takes 8 bytes, and that the value is encoded in two's complement.因为 C 的指定方式允许为遵循您的任何一个假设的平台创建完全一致的实现——long long int需要 8 个字节,并且该值以二进制补码编码。

The standard gives you <limits.h> which will contain the limits of long int on the given platform, LONG_MAX and LONG_MIN , and the only guarantee you are given is that either value will be no less in magnitude than 2147483647 (2^32-1), respectively.该标准为您提供<limits.h> ,其中将包含给定平台LONG_MAXLONG_MINlong int的限制,并且您得到的唯一保证是任一值的大小不小于2147483647 (2^32- 1),分别。

It's a good idea to not make assumptions on platform specifics, and keeping your code portable that way.最好不要对平台细节做出假设,并以这种方式保持代码的可移植性。

There is not really a range that can be specified for float , double and long double , since these are floating point numbers.没有真正可以为floatdoublelong double指定的范围,因为这些是浮点数。 While the distance between integers is always 1, there are different distances between the individual numbers for floating point numbers (the distance increases with larger numbers), and thus also the smallest representable number in terms of amount:虽然整数之间的距离始终为 1,但浮点数的各个数字之间的距离不同(距离随着数字的增大而增加),因此在数量方面也是最小的可表示数字:

  • float is a 32-bit value including a 23 mantissa, 8 bit exponent and 1 sign bit. float是一个 32 位值,包括 23 个尾数、8 位指数和 1 个符号位。 The minimum value here is -3.40∙10³⁸ and the maximum 3.40∙10³⁸.这里的最小值为 -3.40∙10³⁸,最大值为 3.40∙10³⁸。 The smallest representable value in amount is 1.18∙10⁻³⁸.数量的最小可表示值是 1.18∙10⁻³⁸。
  • double is a 64-bit value including a 52 mantissa, 11 bit exponent and 1 sign bit. double是一个 64 位值,包括 52 个尾数、11 位指数和 1 个符号位。 The minimum value here is 1.79∙10³⁰⁸ and the maximum 1.79∙10³⁰⁸.这里的最小值是 1.79∙10³⁰⁸,最大值是 1.79∙10³⁰⁸。 The smallest representable value in amount is 2.23∙10⁻³⁸.量的最小可表示值是 2.23∙10⁻³⁸。
  • long double is a 80-bit value including a 64 mantissa, 15 bit exponent and 1 sign bit. long double是一个 80 位值,包括 64 个尾数、15 位指数和 1 个符号位。 The minimum value here is -1.18∙10⁴⁹³² and the maximum 1.18∙10⁴⁹³².这里的最小值为 -1.18∙10⁴⁹³²,最大值为 1.18∙10⁴⁹³²。 The smallest representable value in amount is 3.37∙10⁻⁴⁹³².量的最小可表示值是 3.37∙10⁻⁴⁹³²。

Addendum: The values also depend on the platform, the values above apply to the platform.附录:数值也取决于平台,以上数值适用于平台。 There is no 80-bit floating point type under . 下没有 80 位浮点类型。


References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM