我是 C 的新手,在使用模数时遇到问题

I am new to C and having trouble with using modulus

在 'mod = number%iter;' 这行,我可以用任何其他操作替换模数并且它有效,但不能用模数。有人知道为什么吗?它只执行前两次 printf 然后说

RUN FAILED (exit value: 1, total time: 1s)

以后还会有更多的代码,所以这个功能现在可能没用,但以后不会了。 请记住,我是全新的,所以请不要为我的小脑袋把它复杂化!这是代码:

#include <stdio.h>
#include <string.h>
int test(int number) {
    printf("Number = %d\n",number);
    int iter;
    int mod;
    for (iter=0;iter<number;iter+=1) {
        printf("%d ",iter);
        printf("%d ",number);
        mod = number%iter;
        printf("%d\n",mod);
    }
    return 0;
}
int main(){
    printf("C never works\n");
    test(10);
}

使用取模运算符可以得到 left-hand 操作数除以 right-hand 操作数后的余数。您的循环从 iter = 0 开始,您执行:

mod = number%iter;

除以零。使用取模运算符意味着进行除法并检查余数。至少在我的编译器上它指定了异常是什么。

Unhandled exception at -------- Integer division by zero

除以零不仅在编程中没有定义,在数学中也是如此。你不能除以零。喜剧演员 Steven Wright 说 "Black holes are where God divided by zero." 因此请相应地调整您的代码。