我是 C 的新手,我遇到了一个我无法理解的分段错误。我有以下程序,它试图计算严格正数的因子数:
#include <stdio.h>
#include <math.h>
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
int check (int x) {
if (x>0) {
return 1;
} else {
return 0;
}
}
int main(void) {
int number;
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
}
return 0;
}
在 main() 中的这些行之后输入整数和 ENTER 后立即发生分段错误:
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
这些行中的什么导致了分段错误,我该怎么做才能避免它?