c - 我该如何处理垃圾?

标签 c

我是 C 新手。我正在尝试编写一个程序来计算斜边,但是当我运行它时,我得到了垃圾值,并且我不知道如何处理它。

double Hypotenuse(double base, double perpendicular)
{
  double hyp = sqrt((base*base) + (perpendicular*perpendicular));

  return hyp;
}

int _tmain(void)
{
  double b, p;

  printf("Enter the lenght of base\n");
  scanf_s("%f",&b);

  printf("Enter the lenght of perpendicular\n");
  scanf_s("%f",&p);

  printf("The hypotenuse of the triangle is %3.3f",Hypotenuse(b,p));

  return 0;
}

最佳答案

问题出在你的 scanf 语句中。 作为一条经验法则,您应该始终对 double 使用“%lf”。 这非常有效:

double Hypotenuse(double base, double perpendicular)
{
  double hyp = sqrt((base*base) + (perpendicular*perpendicular));

  return hyp;
}

int _tmain(void)
{
  double b, p;

  printf("Enter the lenght of base\n");
  scanf_s("%lf",&b);

  printf("Enter the lenght of perpendicular\n");
  scanf_s("%lf",&p);

  printf("The hypotenuse of the triangle is %3.3lf",Hypotenuse(b,p));

  return 0;
}

关于c - 我该如何处理垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135210/

相关文章:

c - 如何从用户空间 panic 内核

c - libuv 是否提供任何设施来将缓冲区附加到连接并重新使用它

c - 使用c构建 float

c - 如何从 getaddrinfo() 打印 IP 地址

c - 如何获取多维数组中的数组数量

c - 为 ARMV7A 处理器在用户模式下启用中断

c++ - GSL-GNU 中的矩阵乘法

c++ - MessageBox "Abnormal program termination"让我的应用程序保持运行

c++ - realloc 错误而不是 malloc?

c - 了解声明 C 字符串的两种方式