c - 以下错误消息 "error: invalid operands to binary expression (' double' 和 'double' )” 是什么意思?

标签 c debugging cs50

当我尝试在 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/

相关文章:

c - C(gcc) 编译器中的递增和递减

c - 这个循环在一个周期内执行了多少条指令?

ios - 使用 Xcode Instruments 查看指针处的非僵尸对象历史记录

c - 尝试配置程序从 C 中的字符串返回缩写,遇到段错误

c - cs50拼写器分配中的无限循环

c - 如何向终端输出不同深浅的绿色?

c - Linux 2.6.x 中线程的动态优先级是如何计算的?

c - Valgrind(低于主要),如何获得完整的堆栈跟踪?

amazon-web-services - AWS Step Functions 和 Fargate 任务 : container runtime error does not cause pipeline to fail

c - 调整大小程序适用于某些人,但不适用于其他人