c - 表达式必须是可修改的值 (C)

标签 c loops expression unmodifiable

快速提问:

#include <stdio.h>

int main(void) {
    int divisor, counter, binary, counter2;
    int digit0, digit1, digit2, digit3; 
    float decimal;

    printf("Decimal\t\tBinary\n");

    for (counter = 0; counter <= 15; counter++) {
        printf("%d\t\n", counter);   

        decimal = counter;

        for (counter2 = 0; counter2 <= 3; counter2++) {
            decimal % 2 == 1 ? digit0 = 1 : digit0 = 0);
        }
    }

    return 0;
}

我在第二个 for 循环中不断收到变量名“decimal”的“表达式必须是可修改值”的错误。

这是为什么,我该如何解决?

谢谢!

最佳答案

因为decimalfloat ,但是%仅适用于整数。如果您确实想使用 float 进行 mod ,您可以使用函数float fmod(float x, float y) ,它计算 x%y ,并且您应该包括 #include <math.h>使用它。

关于c - 表达式必须是可修改的值 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166461/

相关文章:

.each 循环中的 Ruby 性能

java - cup 解析器允许我在语法 LALR 中进行空转换吗?

c - 下面的格式说明符在做什么?

c - 调试 C 中的字符串替换函数

c - 如何在 FreeRTOS 中重新启动任务

c - 寻找一个简单但棘手的程序的输出

spring - bean定义中使用的Spring表达式语言(SpEL)中的条件

c - atof 未在单链表中转换

c++ - 每秒编码 60 次 C++

python - 在函数内编写循环会改变输出