当我除掉两个变量时,C程序输出一个错误

当我除掉两个变量时,C程序输出一个错误,c,math,C,Math,我正试图制作一个程序,通过让用户输入两个数字area和width来输出矩形的高度,然后将这两个变量area和width除以得到高度的结果。当运行这个程序时,结果是0.0000 我认为这可能与我的scanf或printf行的转换说明符有关 我只是在学习C,所以很难弄清楚问题是什么 #include <stdio.h> int main() { /* initilize variables (area, width and height) */ float area,

我正试图制作一个程序,通过让用户输入两个数字area和width来输出矩形的高度,然后将这两个变量area和width除以得到高度的结果。当运行这个程序时,结果是0.0000

我认为这可能与我的scanf或printf行的转换说明符有关

我只是在学习C,所以很难弄清楚问题是什么

#include <stdio.h>

int main()
{
    /* initilize variables (area, width and height) */
    float area, width, height;
    /* assign variables */
    area = 0.0;
    width = 0.0;
    height = 0.0;
    /* Gather user input */
    printf("Enter the area in square metres\n");
    scanf("%f", &area);
    printf("Enter the width in square metres\n");
    scanf("%f", &width);
    /* Height of a rectangle = area/width */
    height = area/width;
    /* Print result */
    printf("The height of the rectangle is: %f", &height);

    return 0;
}

那只是一个小小的错误

#include <stdio.h>

int main() {
    /* initilize variables (area, width and height) */
    float area, width, height;
    /* assign variables */
    area = 0.0;
    width = 0.0;
    height = 0.0;
    /* Gather user input */
    printf("Enter the area in square metres\n");
    scanf("%f", &area);
    printf("Enter the width in square metres\n");
    scanf("%f", &width);
    /* Height of a rectangle = area/width */
    height = area / width;
    /* Print result */
    printf("The height of the rectangle is: %f", height);

    return 0;
}
错误是一致的

    printf("The height of the rectangle is: %f", &height)
它应该是height而不是&height

换成

    printf("The height of the rectangle is: %f", height)
你可以走了

我现在已经测试过了,很好

Enter the area in square metres
12
Enter the width in square metres
3
The height of the rectangle is: 4.000000

正如在对问题的评论中所指出的,您应该学会使用调试器,然后您将很快看到高度得到了正确的值。所以这个问题与划分两个变量无关,只有您的print语句是错误的。

这基本上是一个打字问题,您正在打印高度变量的地址&height


正如其他人提到的,除了最后的printf行之外,所有代码都是正确的

其他回答建议使用调试器。如果您不熟悉调试器,那么最好的入门方法是使用在线调试器,例如

当调试器点击断点时,您将看到本地变量右侧窗格中的面积、宽度和高度值都在现场


因此,您可以推断问题可能只出现在放置断点的最后一行。

您上次的printf是错误的。您不应该传递高度地址,因此请删除&。我建议您学习如何使用调试器。您很快就会看到高度得到了正确的值。所以这个问题与除法两个变量无关,只有你的print语句是错误的。@MartinR如何使用它,我也想学习,但我不知道最好的来源。非常感谢。我已经在这个问题上纠缠了几个小时,不知道哪里出了问题。@jakeritterhours??
Enter the area in square metres
12
Enter the width in square metres
3
The height of the rectangle is: 4.000000
#include <stdio.h>

int main()
{
    /* initilize variables (area, width and height) */
    float area, width, height;
    /* assign variables */
    area = 0.0;
    width = 0.0;
    height = 0.0;
    /* Gather user input */
    printf("Enter the area in square metres\n");
    scanf("%f", &area);
    printf("Enter the width in square metres\n");
    scanf("%f", &width);
    /* Height of a rectangle = area/width */
    height = area/width;
    /* I have just changed &height to height  */
     printf("The height of the rectangle is: %f", height);

    return 0;
}