递归因子函数无法正常工作

mhm*_*mhm 3 c++ recursion

为什么这个递归函数最多只能计算(20!)?当我输入21时,它显示意外的结果.

#include <iostream>
using namespace std;

long long int factorial( long long int number )
{
    if( number <= 1 )
        return 1;

    return number * factorial( number - 1 );
Run Code Online (Sandbox Code Playgroud)

}

int main()
{
    long long int number;
    while( cin >> number )
            cout << factorial( number ) << endl; // factorial( 20 ) = 2432902008176640000
                                                 // factorial( 21 ) = -4249290049419214848 ????
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ber*_*ann 6

21的阶乘是51090942171709440000.您计算机上签名的长签最长可以2^63-1 = 9223372036854775807.

2432902008176640000    20 factorial
9223372036854775807    2^63-1 (the maximum for a long long on your computer)
51090942171709440000   21 factorial
Run Code Online (Sandbox Code Playgroud)

当数字大于最大值时,行为未定义.大多数计算机上发生的事情是它包含最负数.