带有嵌套 for 循环的简单 C 程序在 windows powershell 中停止工作

Simple C program with nested for loops stops working in windows powershell

我在 powershell 中成功编译了一个简单的 C 代码,问题是每当我输入一个数字以满足我为循环设置 运行 的条件时,程序停止工作( windows 10 对话框出现,通知我它正在尝试找到解决问题的解决方案和所有)

此外,我在 windows10 中使用 powershell(x86) 控制台编译了代码,这会导致问题吗?

#include <stdio.h>

int main(){
    int x, y, j, i, foo;
    int count = 0, prim[1000];
    printf("Enter number: ");
    scanf("%d", &x);

for(y = 1; y < x; y++){
    for(j = 1; j <= y; j++){
        foo = j - 1;
        if (y % foo  == 0){
            count++;        
        }   
      }
    printf("%d", count);
  }
}

尝试使用#include < conio.h> 和 getch();在最后一次使用 printf 之后。如果这不起作用,则应该是您使用的编译器有问题。

您的程序失败,因为在内循环的第一次迭代中 foo==0。

for(j = 1; j <= y; j++){ foo = j - 1; if (y % foo == 0){ ---> y % 0 --> 未定义的行为。

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined. C11dr §6.5.5 5

不要那样做。