【问题标题】:Why does my code only produce a 0.000000 outcome? [duplicate]为什么我的代码只产生 0.000000 的结果? [复制]
【发布时间】:2018-04-24 09:40:21
【问题描述】:

这是我正在使用的代码,

#include <stdio.h>
int main () {
    /* variable definition: */
    double sidea, sideb, sidec;
    /* Prompt user for side a */
    printf("Enter length of side a of the triangle: \n"); // Input the base
    scanf("%f", &sidea);
    /* Prompt user for side b */
    printf("Enter the length of side b of the triangle: \n"); // Input the base
    scanf("%f", &sideb);
    /* Prompt user for side c */
    printf("Enter the length of side c of the triangle \n");
    scanf("%f", &sidec);
    // Calculate the Perimeter
    double perimeter=  (sidea + sideb + sidec);
    // Print the result
    printf("The perimeter is : %f \n", perimeter);
    return 0;
}

输入数据: 4.6 7.5 5.8

我一生都无法弄清楚为什么用于输入的任何值都会导致 0.000000。使用浮动似乎工作得很好。我错过了什么。谢谢!

【问题讨论】:

  • 请查看How to Ask,然后编辑您的问题标题,使其看起来更像一个实际问题。
  • 另外,显示引起您关注的输入数据。
  • 谢谢你,我很抱歉。

标签: c printf double


【解决方案1】:

使用%lf 格式说明符读取双精度。

您还可能注意到使用错误的格式说明符是 UB。(未定义的行为)。

【讨论】:

    猜你喜欢
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多