gpt4 book ai didi

c - C中的长数据类型计算

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:38 24 4
gpt4 key购买 nike

我有一个简单的程序可以找到给定数字的自守数。问题是它只是找到 square 参数绑定(bind)到 int 的数字。

例如:90625 x 90625 = 8212890625 但程序找不到它。

问题:long类型这里有什么问题?它表现得像 int。是否在代码的任何位置将 square 参数强制转换为 int?

#include <stdio.h>
int main(void)
{
int n;
long i;

printf ("Enter a value:\n") ;
scanf ("%d",&n) ;
printf ("Automorphic numbers:\n") ;
for (i=1L ; i<=n ; i = i+1L)
{
long square = i*i ;

if (square % 10L == i || square % 100L == i || square % 1000L == i || square % 10000L == i || square % 100000L == i || square % 1000000L == i || square % 10000000L == i)
printf ("%li\n", i) ;
}
}

最佳答案

C 保证以下限制(n869 C99 草案):

-- minimum value for an object of type int
INT_MIN -32767 // -(215-1)

-- maximum value for an object of type int
INT_MAX +32767 // 215-1

-- maximum value for an object of type unsigned int
UINT_MAX 65535 // 216-1

-- minimum value for an object of type long int
LONG_MIN -2147483647 // -(231-1)

-- maximum value for an object of type long int
LONG_MAX +2147483647 // 231-1

-- maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 232-1

-- minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // -(263-1)

-- maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 263-1

[5.2.4.2.1 Sizes of integer types <limits.h>]

当您需要定义大小时,请使用 uint64_t 和类似的数据类型。

关于c - C中的长数据类型计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com