因子计算在我的C程序中是不正确的?

App*_*lik 3 c factorial

#include <stdio.h>
#include <conio.h>

int main()
{
  long signed fact=1;

  int c, n ;

  printf("Factorial to be calculated: ");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
  fact = fact * c;

  printf("Factorial of %d = %ld\n", n, fact);
  getch();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在上面的C程序中,当我跑步时,我无法得到13!正确.它的输出是真的12.如何解决这个问题?我认为很久没有签名就足够了13.

Oli*_*rth 6

long您的平台上可能是32位(您可以通过打印值来查找sizeof(long)).13!大于2 ^ 32-1(32位无符号值的最大可能值),因此溢出.

尝试使用uint64_t(from <stdint.h>)代替.