-3

使用 xcode 编程 C,这里是 f

#include <stdio.h>

int multiply (int x, int y) {
return x * y;
}
int main()
{

float result = (float) multiply (0.2, 0.5);
printf("the result is %f", result);

}

我没有得到正确的值,我得到 0.0000 !我做了演员,但我不知道出了什么问题。

4

3 回答 3

6

你的程序0乘以0.

multiply接受两个int参数,因此在调用之前你的0.20.5被隐式转换为int。这会将两者都截断为0.

你的类型转换在这个程序中没有做任何事情multiply,因为(which is an )的返回值int将在赋值期间被隐式转换为result反正。

如果您希望此程序正常工作,您需要更改multiply(或添加浮点版本并调用它)的定义。

于 2013-05-08T14:42:32.583 回答
2

输入multiply ()参数是int

int multiply (int x, int y) {

并且您已float作为输入参数传递:

multiply (0.2, 0.5);
于 2013-05-08T14:41:56.403 回答
1

Hi there is a basic problem. As the numbers you are multiplying are floats but you are passing these into the function multiply as int's hence being rounded to 1 and 0.

This should work

#include <stdio.h>

int multiply (float x, float y) {
  return x * y; 
}
int main()
{

float result = (float) multiply (0.2, 0.5);
printf("the result is %f", result);
} 
于 2013-05-08T14:47:50.110 回答