Jason
Jason

Reputation: 25

Basic C programming, printf function

while (i) {
    printf("Digit (%d) = %d", d, ((num2/(pow(10,(i-1))))%10));
    d++;
    i--;
}

i and d are int values declared earlier on in the function. The error I'm getting is "Operands of '%' have incompatible types 'double' and 'int'."

I keep getting this error message even after fiddling with the values.

Upvotes: 0

Views: 111

Answers (2)

timrau
timrau

Reputation: 23058

In alternative to casting the numerator into int, you could also call fmod() to perform modular computation in terms of floating point numbers.

printf("Digit (%d) = %lf", d, fmod((num2/(pow(10.0,(i-1)))),10.0));

Upvotes: 3

unxnut
unxnut

Reputation: 8839

That is because pow returns a double. You will have to typecast it. Change the statement to:

printf("Digit (%d) = %d", d, ((int)(num2/(pow(10,(i-1))))%10));

Upvotes: 7

Related Questions