我正在尝试学习 C ,并提出了以下小程序。

#include "stdafx.h"

void main()
{
    double height = 0;
    double weight = 0;
    double bmi = 0;

    printf("Please enter your height in metres\n");
    scanf_s("%f", &height);
    printf("\nPlease enter your weight in kilograms\n");
    scanf_s("%f", &weight);
    bmi = weight/(height * height);
    printf("\nYour Body Mass Index stands at %f\n", bmi);
    printf("\n\n");
    printf("Thank you for using this small program.  Press any key to exit");
    getchar();
    getchar();
}

该程序可以完美编译,但是该程序返回的答案没有任何意义。如果我输入1.8作为高度,输入80作为体重,则bmi就像1.#NF00一样,没有意义。

我究竟做错了什么?

最佳答案

scanf(和scanf_s)格式%f要求指针键入float

只需将heightweight变量的类型更改为float即可解决此问题。

关于c - 编程数据类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11671793/

10-11 21:45
查看更多