双倍之前的预期表达

use*_*353 1 c double

一直说double int pt函数之前有一个预期的表达式

 #include <stdio.h>
 int pt (int a, int b)
 {
    int c, result;
      c = (a * a) + (b + b);
        result = double sqrt (double c);
   return result;
 }
 int main (void)
 {
     int d, e, f;
     int pt (int a, int b);
     printf("type enter after input of the two legs");
     scanf("%i", &d);
     scanf("%i", &e);
     f = pt (d,e);
     printf("the hypotenuse is %i", f);
     return 0;
 }
Run Code Online (Sandbox Code Playgroud)

And*_*_CS 6

改变

result = double sqrt (double c);
Run Code Online (Sandbox Code Playgroud)

result = sqrt(c);
Run Code Online (Sandbox Code Playgroud)

由于隐式转换,当您将其传递给函数时,您不必强制c转换为 a 。如果你还想做演员表,正确的方法是.doublesqrtsqrt((double) c)

此外,#include <math.h>对于该sqrt功能的使用。


注:它不是必需投的返回类型sqrt,以int(相关,因为result是类型int) -但是有些编译器可以给出关于隐式转换的警告(感谢@MattMcNabb)。将演员表放入向其他编码员发出信号,表明精度损失是有意的,这也是一种很好的做法。

  • 不需要演员表;`result = sqrt(c);` 是正确的代码。`double` 和 `int` 之间存在两种隐式转换。在这里使用强制转换的唯一效果是作为一种 hack 来禁用某些编译器发出的非标准警告。 (3认同)