Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 接收简单因子代码中的wierd数学错误,don';我不知道为什么_C_Math - Fatal编程技术网

C 接收简单因子代码中的wierd数学错误,don';我不知道为什么

C 接收简单因子代码中的wierd数学错误,don';我不知道为什么,c,math,C,Math,我在这里做一个简单的因子程序,出于某种原因,它给了我非常奇怪的答案 我不知道我是否做错了什么。谁能解释一下这个错误以及为什么会发生 示例:输入5时返回-1899959296 #include <stdio.h> void factor(int total); int main (){ printf("enter a number \n"); int val; scanf("%i", &val); factor(val); } void factor

我在这里做一个简单的因子程序,出于某种原因,它给了我非常奇怪的答案

我不知道我是否做错了什么。谁能解释一下这个错误以及为什么会发生

示例:输入5时返回-1899959296

#include <stdio.h>


void factor(int total);

int main (){

printf("enter a number \n");

int val;
scanf("%i", &val);


factor(val);


  }

 void factor (int total){

for (int count = 1; count<=total; count++ ){
    total = total * count;
}

printf("%d", total);

}
#包括
空隙系数(整数);
int main(){
printf(“输入数字”);
int-val;
scanf(“%i”和&val);
因子(val);
}
空隙系数(整数总和){

对于(int count=1;count您使用相同的变量来计算导致整数溢出的计算和条件

for (int count = 1; count<=total; count++ ){
   total = total * count;
} 
然后把条件变成这样

 output=output*count;

和print
printf(“%d\n”,output);

for(int count=1;count循环结束的条件是
countGoogle“C中的整数溢出”。
 output=output*count;
for (int count = 1; count<=total; count++ )
{
    total = total * count;
}
unsigned int factor = 1;
    for (count = 1; count <= total; count++ )
    {
        factor = factor * count;
    }
printf("%u\n\n",factor);