gpt4 book ai didi

c - 二进制 % 的无效操作数(有 float 和 float )

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:20 24 4
gpt4 key购买 nike

下面是我的代码:

#include <stdio.h>

int main ( )
{
float a = 5,b = 2;
int c,d;
c = a % b;
d = a/2;
printf("%d\n",d);
return 0;
}

当我尝试编译它时,我得到了

"Invalid operands to binary %"

在第 6 行。我该如何解决这个问题?

最佳答案

 c = a % b;

您不能将 float 用作 % 运算符的操作数。

改用 math.h 中的 fmod

double a=5,b=2,c;
c=fmod(a,b); // it returns double

关于c - 二进制 % 的无效操作数(有 float 和 float ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com