在C中存储大量数字
我有以下代码,其中有一个数组。我向该数组添加了一个很大的数字,但是在打印它时,它显示了一个较小的、不正确的值。为什么会这样,有没有办法解决这个问题?
int x[10];
x[0] = 252121521121;
printf(" %i " , x[0]); //prints short wrong value
I have the following code where I have an array. I add a large number to that array, but when printing it, it shows a smaller, incorrect value. Why is that, and is there a way to fix this?
int x[10];
x[0] = 252121521121;
printf(" %i " , x[0]); //prints short wrong value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的号码需要 38 位。如果您的平台的
int
不是那么大(并且没有理由它应该那么大),那么这个数字根本不适合。 (事实上,即使是 int 文字也应该已经触发编译器警告,假设这是 C 或 C++。)您始终可以使用有保证大小的数据类型,例如 int64 或类似的东西,具体取决于您的语言和平台。这里可能不需要任意精度的库。
在 C 中,
包含使用
并使用int64_t
,或者仅long long int
,并且确保从 long long 整数文字初始化它,例如252121521121LL
。 (我可能会补充说,长整型只是最新语言标准的正式一部分。)(编辑:
long long int
保证至少为 64 位,因此它应该是一个不错的选择。)Your number requires 38 bit. If your platform's
int
isn't that big (and there's no reason it should be), the number simply won't fit. (In fact, even the int literal should already have triggered a compiler warning, supposing that this is C or C++.)You could always use a data type of guaranteed size, like an int64 or something like that, depending on your language and platform. Probably no need for arbitrary-precision libraries here.
In C,
includeuse<stdint.h>
and useint64_t
, or justlong long int
, and make sure you initialize it from a long long integer literal, e.g.252121521121LL
. (Long longs are only officially part of the most recent language standards, I might add.)(Edit:
long long int
is guaranteed to be at least 64 bit, so it should be a good choice.)在大多数系统上,
int
是 32 位。这足以存储大约 20 亿个已签名的数字,或 40 亿个未签名的数字。要存储更大的数字,您需要更大形式的 int。 (不幸的是,在某些系统上,long int
与int
相同——很好的标准化——所以你需要使用long long int如果您可以在库中找到 typedef(例如
int64_t
),那就更好了。)An
int
, on most systems, is 32 bits. That's enough to store a number of about 2 billion signed, or 4 billion unsigned. To store larger numbers you need a larger form of int. (Unfortunately, on some systems along int
is the same as anint
-- good ol' standardization -- so you need to go to along long int
. Better if you can find a typedef in your library such asint64_t
.)如果您只遇到这个特定数字的问题,那么只需按照之前答案中的建议使用
long long int
即可。否则,对于更大的数字(
>1E19
对于有符号数字),您可能需要切换到大数字库或自己编写这种数据类型。您基本上需要将数字的每个数字存储在数组(或链表)中,并手动编写所需的基本操作:加、减、乘等。一些库包括
https://mattmccutchen.net/bigint/
或 GMP。
If you only have the problem with this particular number, then just use a
long long int
as suggested in previous answers.Otherwise, for even larger numbers (
>1E19
for signed numbers), you might want to switch to a large number library or code yourself this kind of data type. You basically need to store each digit of your number in an array (or linked list) and manually code basic operations you need on them : adding, subtracting, multiplying etc.Some libraries include
https://mattmccutchen.net/bigint/
or GMP.
好吧,你的数字似乎超出了 32 位整数可以容纳的最大值..
Well, your number just seems to exceed the maximum value a 32bit integer can hold..