当我尝试在 C 中使用 float 作为输入时实现“%”运算符时,就会发生这种情况。 我注册了 cs50 类(class),这是我正在尝试的问题集 1 的第四个问题。
这是代码
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("O hai! How much change is owed?\n");
float x = get_float();
if(x % 0.25 == 0)
{
printf("%.55f\n", x/0.25);
}
else if(x % 0.10 == 0)
{
printf("%.55f\n", x/0.10);
}
else if(x % 0.05 == 0)
{
printf("%.55f\n", x/0.05);
}
else if(x % 0.01 == 0)
{
printf("%.55f\n", x/0.01);
}
}
最佳答案
模运算符 (%) 返回除法运算的余数,仅适用于整数(C 等效数据类型 int、short、long)。由于您尝试对非整数( float )执行此操作,编译器不允许您编译程序并引发错误。
关于c - 以下错误消息 "error: invalid operands to binary expression (' double' 和 'double' )” 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45429211/