Search code examples
cloopsexpressionunmodifiable

Expression Must Be Modifiable Value (C)


Quick question:

#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;
}

I keep getting the error that the "expression must be a modifiable value" on variable name "decimal" in the second for loop.

Why is this, and how can I fix it?

Thank you!


Solution

  • Because decimal is float,but % only for integers.If you really want to mod by using float, you can use function float fmod(float x, float y), it calculates x%y, and you should include #include <math.h> to use it.