[英]C: for loop with two nested for loops stops working after first cycle
[英]Simple C program with nested for loops stops working in windows powershell
我在powershell中成功地編譯了一個簡單的C代碼,問題是每當我輸入一個數字,以便滿足我為要運行的for循環設置的條件時,程序就會停止工作(顯示Windows 10對話框,通知我,它正在嘗試找到解決問題的解決方案等等)
另外,我在Windows 10中使用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);
}
}
for(j = 1; j <= y; j++){ foo = j - 1; if (y % foo == 0){
for(j = 1; j <= y; j++){ foo = j - 1; if (y % foo == 0){
---> y % 0
>未定義行為。
/運算符的結果是第一個操作數除以第二個的商。 %運算符的結果是余數。 在這兩個操作中, 如果第二個操作數的值為零,則行為是不確定的。 C11dr§6.5.55
不要那樣做
嘗試使用#include <conio.h>和aslo getch(); 最后一次使用printf之后。 如果這不起作用,則說明您使用的編譯器存在問題。
您的程序失敗,因為在內循環的第一次迭代中foo == 0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.