C 为什么我会得到这些随机值?

C 为什么我会得到这些随机值?,c,function,pointers,C,Function,Pointers,好的,我必须编写一个函数,它使用指针将天转换成年、周和天。下面是函数 int convertTime(int days, int *y, int *w, int *d){ if (days < 0 || y == NULL || w == NULL || d == NULL){ printf("An error has occured\n"); return 1; }else{ *y = days / 365; *w = (days % 365) /

好的,我必须编写一个函数,它使用指针将天转换成年、周和天。下面是函数

int convertTime(int days, int *y, int *w, int *d){
  if (days < 0 || y == NULL || w == NULL || d == NULL){
    printf("An error has occured\n");
    return 1;
  }else{
    *y = days / 365;
    *w = (days % 365) / 7;
    *d = ((days % 365) / 7) % 7)
    return 0;
  }
}
然后打印出来

Expected output: 2 years, 38 weeks, 4 days
Actual output: -127184896 years, -132560896, weeks, -135499072 days

您忘记将变量传递给printf。按如下方式修复最后一行:

printf("Actual output: %d years, %d, weeks, %d days\n", y2, w2, d2);

如其他答案中所述,您需要将值传递给printf


您还需要检查convertTime的返回值

编译器应该警告您缺少printf参数,例如gcc、clang,默认情况下这样做。
printf("Actual output: %d years, %d, weeks, %d days\n", y2, w2, d2);