C语言 C Int 和 Long 32 - 64 位中的值范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6155784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:47:12  来源:igfitidea点击:

Range of values in C Int and Long 32 - 64 bits

cint32bit-64bit

提问by Custodio

I'm confused with range of values of Int variable in C.

我对 C 中 Int 变量的值范围感到困惑。

I know that a 32bits unsigned int have a range of: 0 to 65,535. So long has 0 to 4,294,967,295

我知道 32 位 unsigned int 的范围是:0 到 65,535。这么久有 0 到 4,294,967,295

This is fine in 32bits machine. But now in 64bits machines all thing keep the same? Or maybe my int capacity is different?

这在 32 位机器上很好。但是现在在 64 位机器中所有的东西都保持不变?或者也许我的int容量不同?

I understand this questions as newbie, but I'm really confused. This method signature is not helping too. :)

作为新手,我理解这个问题,但我真的很困惑。此方法签名也无济于事。:)

unsigned long long int atomicAdd(unsigned long long int* address, unsigned long long int val);

回答by Johannes Schaub - litb

In C and C++ you have these least requirements (i.e actual implementations can have larger magnitudes)

在 C 和 C++ 中,您有这些最少的要求(即实际实现可以有更大的幅度)

signed char: -2^07+1 to +2^07-1
short:       -2^15+1 to +2^15-1
int:         -2^15+1 to +2^15-1
long:        -2^31+1 to +2^31-1
long long:   -2^63+1 to +2^63-1

Now, on particular implementations, you have a variety of bit ranges. The wikipedia articledescribes this nicely.

现在,在特定实现上,您有多种位范围。在维基百科的文章介绍了这个好听。

回答by T.E.D.

No, intin C is notdefined to be 32 bits. intand longare not defined to be any specific size at all. The only thing the language guarantees is that sizeof(char)<=sizeof(short)<=sizeof(long).

不,int在 C 中没有定义为 32 位。int并且long根本没有定义为任何特定大小。该语言唯一保证的是sizeof(char)<=sizeof(short)<=sizeof(long).

Theoretically a compiler could make short, char, and longall the same number of bits. I know of some that actually did thatfor all those types save char.

理论上编译器可以使shortcharlong所有相同的比特数。我知道有些人实际上对所有这些类型都这样做了save char

This is why C now defines types like uint16_tand uint32_t. If you need a specific size, you are supposed to use one of those.

这就是为什么 C 现在定义了像uint16_t和这样的类型uint32_t。如果您需要特定尺寸,则应该使用其中之一。

回答by Ely

Excerpt from K&R:

摘自 K&R:

shortis often 16 bits, long32 bits and inteither 16 bits or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and shortis no longer than int, which is no longer than long.

short通常是 16 位、long32 位和int16 位或 32 位。每个编译器都可以自由地为自己的硬件选择合适的大小,只受shorts 和ints 至少为 16 位,longs 至少为 32 位的限制,并且short不超过int,不超过long



You can make use of limits.hthat contains the definition of the limits for the decimal/float types:

您可以使用limits.h包含小数/浮点类型限制的定义:

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


int main(int argc, char** argv) {

    printf("CHAR_BIT    :   %d\n", CHAR_BIT);
    printf("CHAR_MAX    :   %d\n", CHAR_MAX);
    printf("CHAR_MIN    :   %d\n", CHAR_MIN);
    printf("INT_MAX     :   %d\n", INT_MAX);
    printf("INT_MIN     :   %d\n", INT_MIN);
    printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
    printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
    printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
    printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
    printf("SHRT_MAX    :   %d\n", SHRT_MAX);
    printf("SHRT_MIN    :   %d\n", SHRT_MIN);
    printf("UCHAR_MAX   :   %d\n", UCHAR_MAX);
    printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
    printf("ULONG_MAX   :   %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX   :   %d\n", (unsigned short) USHRT_MAX);
    printf("FLT_MAX     :   %g\n", (float) FLT_MAX);
    printf("FLT_MIN     :   %g\n", (float) FLT_MIN);
    printf("-FLT_MAX    :   %g\n", (float) -FLT_MAX);
    printf("-FLT_MIN    :   %g\n", (float) -FLT_MIN);
    printf("DBL_MAX     :   %g\n", (double) DBL_MAX);
    printf("DBL_MIN     :   %g\n", (double) DBL_MIN);
    printf("-DBL_MAX     :  %g\n", (double) -DBL_MAX);

    return (EXIT_SUCCESS);
}

Maybe you might have to tweak a little bit on your machine, but it is a good template to start to get an idea of the (implementation-defined) min and max values.

也许您可能需要在您的机器上稍微调整一下,但这是一个很好的模板,可以开始了解(实现定义的)最小值和最大值。

回答by Joe

There's no one answer. The standard defines minimum ranges. An int must be able to hold at least 65535. Most modern compilers however allow ints to be 32-bit values. Additionally, there's nothing preventing multiple types from having the same capacity (e.g. int and long).

没有一个答案。该标准定义了最小范围。int 必须至少能够容纳 65535。然而,大多数现代编译器允许 int 为 32 位值。此外,没有什么可以阻止多种类型具有相同的容量(例如 int 和 long)。

That being said, the standard does say in your particular case:

话虽如此,标准确实在您的特定情况下说:

0 → +18446744073709551615

as the range for unsigned long long int.

作为 unsigned long long int 的范围。

Further reading: http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Size

进一步阅读:http: //en.wikipedia.org/wiki/C_variable_types_and_declarations#Size

回答by Henno Brandsma

In fact, unsigned int on most modern processors (ARM, Intel/AMD, Alpha, SPARC, Itanium ,PowerPC) will have a range of 0 to 2^32 - 1 which is 4,294,967,295 = 0xffffffff because int (both signed and unsigned) will be 32 bits long and the largest one is as stated.

事实上,大多数现代处理器(ARM、Intel/AMD、Alpha、SPARC、Itanium、PowerPC)上的 unsigned int 将具有 0 到 2^32 - 1 的范围,即 4,294,967,295 = 0xffffffff 因为 int(有符号和无符号)将长度为 32 位,最大的一位如所述。

(unsigned short will have maximal value 2^16 - 1 = 65,535 )

(unsigned short 将具有最大值 2^16 - 1 = 65,535 )

(unsigned) long long int will have a length of 64 bits (long int will be enough under most 64 bit Linuxes, etc, but the standard promises 64 bits for long long int). Hence these have the range 0 to 2^64 - 1 = 18446744073709551615

(unsigned) long long int 的长度为 64 位(long int 在大多数 64 位 Linux 等下就足够了,但标准承诺 long long int 为 64 位)。因此,这些范围为 0 到 2^64 - 1 = 18446744073709551615

回答by Vipin Diwakar

In C and C++ memory requirements of some variable :

在某些变量的 C 和 C++ 内存要求中:

signed char: -2^07 to +2^07-1
short: -2^15 to +2^15-1
int: -2^15 to +2^15-1
long: -2^31 to +2^31-1
long long: -2^63 to +2^63-1

signed char: -2^07 to +2^07-1
short: -2^15 to +2^15-1
int: -2^15 to +2^15-1
long: -2^31 to +2^31-1
long long: -2^63 to +2^63-1

signed char: -2^07 to +2^07-1
short: -2^15 to +2^15-1
int: -2^31 to +2^31-1
long: -2^31 to +2^31-1
long long: -2^63 to +2^63-1

signed char: -2^07 to +2^07-1
short: -2^15 to +2^15-1
int: -2^31 to +2^31-1
long: -2^31 to +2^31-1
long long: -2^63 to +2^63-1

depends on compiler and architecture of hardware

取决于编译器和硬件架构

The international standard for the C language requires only that the size of short variables should be less than or equal to the size of type int, which in turn should be less than or equal to the size of type long.

C语言的国际标准只要求short变量的大小要小于等于int类型的大小,而int类型的大小又要小于或等于long类型的大小。

回答by Paul Rubel

Take a look at limits.h. You can find the specific values for your compiler. INT_MIN and INT_MAX will be of interest.

看看limits.h。您可以找到编译器的特定值。INT_MIN 和 INT_MAX 会很有趣。

回答by phoxis

Have a look at the limits.hfile in your system it will tell the system specific limits. Or check man limits.hand go to the "Numerical Limits" section.

查看系统中的limits.h文件,它会告诉系统特定的限制。或者检查man limits.h并转到“数值限制”部分。

回答by Jerry Coffin

A 32-bit unsigned int has a range from 0 to 4,294,967,295. 0 to 65535 would be a 16-bit unsigned.

32 位无符号整数的范围是 0 到 4,294,967,295。0 到 65535 将是 16 位无符号。

An unsigned long long (and, on a 64-bit implementation, possibly also ulong and possibly uint as well) have a range (at least) from 0 to 18,446,744,073,709,551,615 (264-1). In theory it could be greater than that, but at least for now that's rare to nonexistent.

一个 unsigned long long (以及,在 64 位实现中,可能还有 ulong 和可能的 uint )的范围(至少)从 0 到 18,446,744,073,709,551,615 (2 64-1)。从理论上讲,它可能比这更大,但至少目前这种情况很少见,甚至不存在。

回答by user3240746

It is better to include stdlib.h. Since without stdlibg it takes long as long

最好包括stdlib.h. 由于没有 stdlibg 需要很长时间