简单的 for 循环突然使 C 中的程序崩溃

use*_*465 0 c loops modulo

我通过使用 MSVC

cl file.c
Run Code Online (Sandbox Code Playgroud)

用这个非常简单的代码:

#include <stdio.h>
#include <stdlib.h>
    
int
main(void) {
    
    int num = 50, pointNum = 60, max = 0;
    
    puts("Hello");

    for (int i = 0; i <= num; i++) {
        printf("%d\n", pointNum % i);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我从命令行运行它时,它只是暂停然后崩溃,只打印“Hello”。我不知道出了什么问题,因为它似乎没有错误。

Bil*_*nch 7

当 时i == 0,您执行60 % 0触发除以零的数学运算。这会触发未定义的行为,这通常会使您的程序崩溃。