c - printf() 函数出错

标签 c

#include <stdio.h>

#define MINUTES_IN_HOUR  60
int main() {
    // Two trains will pass each other at a certain time whne they are at an initial fixed distance apart.
    // When will they pass each other and how much distance did each train have to travel?
    // 1 corresponds to train 1, and 2 corresponds to train 2. 

    float distance_1_mile, distance_2_mile, distance_total_mile, rate_1_mph, rate_2_mph, time_minute;

    time_minute = (distance_total_mile * MINUTES_IN_HOUR) / (rate_1_mph + rate_2_mph);
    distance_1_mile = rate_1_mph * time_minute / MINUTES_IN_HOUR;
    distance_2_mile = rate_2_mph * time_minute / MINUTES_IN_HOUR;

    printf("The rate of train 1 and train 2 respectively are what values?\n");
    scanf("%f%f", &rate_1_mph, &rate_2_mph);

    printf("What was the total distance the trains were apart initially?\n");
    scanf("%f", &distance_total_mile);

    printf("The time it takes both trains to arrive side-by-side is %.3f minutes.\n", time_minute);

    printf("The distance train 1 had to travel was %.2f miles.\n",  distance_1_mile);
    printf("The distance train 2 had to travel was %.2f miles.\n", distance_2_mile);

    return 0;
}

我收到的错误是:“警告:格式“%f”需要“double”类型的参数,但参数 2 的类型为“int *”

我尝试在这个网站上查找这个问题,我看到同一问题有两种不同的解决方式,但似乎都不起作用。我也没有发布完整的代码,因为我看到的另一个例子也没有发布,并且他们的问题得到了回答。

最佳答案

编译器告诉你问题是什么,我假设你在某个地方有一行看起来像这样的行

int rate_1_mph, rate_2_mph, distance_total_mile, time_minute;

将 int 更改为 float。

完成此操作后,您需要将 printf 语句中的所有 %d 更改为 %f。 %d 实际上用于整数,而不是 double 。

关于c - printf() 函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890747/

相关文章:

c - C 中的可变参数在 Valgrind 中创建错误

c - 外部变量如何在共享库中工作

c - C程序中结构的生命周期

c++ - execv,选择并阅读

c - C中计算任意函数1到10的函数

c - 使用 Matlab Coder 生成的算法进行生产

c - Linux 上的 C 多线程信号处理

c - 如何改变C中输出的颜色

c - 将链表数组传递给函数

c - Mingw 和 _stati64()