二进制 % 的无效操作数(有浮点型和浮点型)

0 c modulo

下面是我的代码:

#include <stdio.h>

int main ( )   
{
    float a = 5,b = 2;    
    int c,d;    
    c = a % b;
    d = a/2;    
    printf("%d\n",d);    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这个时,我得到

“二进制 % 的操作数无效”

在6号线上。我该如何解决这个问题?

ame*_*yCU 5

 c = a % b;
Run Code Online (Sandbox Code Playgroud)

您不能将float其用作运算符的%操作数。

使用fmodfrommath.h代替。

double a=5,b=2,c;
c=fmod(a,b);                 // it returns double
Run Code Online (Sandbox Code Playgroud)