在 GCC 编译器上使用 C 查找因子

finding factors using C on GCC compiler

我写这个函数是为了找到整数的因子, 当我使用 GCC 编译时,我收到一条警告说 "conflicting types for 'factors' "

#include <stdio.h>
#include <stdlib.h>



main(){
    factors(18);
}

void factors(int x){
  int i = 1;
  while (i<=x){
    if (i%x == 0)
       printf("%d \t", i);
    i++;
  }
  printf("\n");
} 

main() 必须有一个 return 类型,默认是 int 所以改变它并且不要忘记最后的 return 语句。

另外你必须在main之前放一个函数原型,所以如果你编译和调用这个函数它是已知的。或者您必须将函数声明放在 main 之前。

#include <stdio.h>
#include <stdlib.h>

/*function prototype*/
void factors(int x);

int main() {
//^ return type 

    factors(18);

    return 0;
  //^^^^^^^^^ return to end the program 'nicely'

}

void factors(int x) {

    int i = 1;

    while (i <= x) {
        if (i % x == 0)
            printf("%d \t", i);

        i++;
    }

    printf("\n");

}