c - 想知道为什么这段代码会导致无限循环

标签 c

我一直在尝试获取数字的阶乘 (f.ex: 4!= 24 [4 (3) (2) (1) = 24])。

然后我写了下面的代码:

#include <stdio.h>

void TestAcomulador(int a, int factorizado, int *resultado);

int main(void) {
    int a, resultado, factorizado, sacaroperacion;
    printf("Introduzca un numero: ");
    scanf("%d", &a);
    TestAcomulador(a, factorizado, &resultado);
    printf("%d", resultado);
}

void TestAcomulador(int a, int numprincipal, int *factorizado) {
    numprincipal = --a;
    do {
        *factorizado = numprincipal * a;
        printf("The loop is here\n"); //LOOP IN THIS LINE
    } while (numprincipal > 0);
}

这是怎么回事,我做错了什么?帮助将不胜感激,以避免在未来遇到这个问题,

提前致谢。

最佳答案

您在任何时候都不会递减 numprincipal,因此它总是大于 0(假设您 a>=2)。

所以 while 循环的条件总是为真。

关于c - 想知道为什么这段代码会导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52370770/

相关文章:

c - 使用仅在运行时初始化的函数指针解析 [-Werror=maybe-uninitialized]

c++ - 将终端设置为原始模式 : Character is displayed only after next key pressed

c - 在 Ansi-C 中导入 Delphi Dll

c - C 语言中的哈希数组技巧

c - 当出现 "multidigit number with all digits identical"时,如何让我的程序执行任何操作?

将命令通过管道传递到 shell 后,C 无限循环

c - 内存违规的奇怪行为

objective-c - 匿名与定义枚举

c - C中的多管道bash风格

c - 夹板:for 循环中的解析错误