简体   繁体   中英

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.

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));

除了将分子转换为int ,还可以调用fmod()来根据浮点数执行模块化计算。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM