C 中浮点值的 mod 运算符。这显示错误"illegal use of floating point"


#include<stdio.h>
#include<conio.h>
void main()
{
  float n, r;
  printf("n enter a number");
  scanf("%d",&n);
  r=n%10;
  n/=10;
  printf("%d %d",n, r );
  getch();
}

此代码在编译时显示错误。我想知道:我们可以在浮点值上执行mod操作吗?

不,你不能在float上使用 %

改用fmod功能这里引用:http://www.cplusplus.com/reference/cmath/fmod/

fmodfhttp://en.cppreference.com/w/c/numeric/math/fmod

r = fmod (n, 10)

您需要#include <math.h>才能使用此功能

另外,您应该执行scanf(%f, &n)以在float

中阅读
#include<stdio.h>
#include<math.h>
int main()
{
  float n, r;
  printf("n enter a number");
  scanf("%f", &n);
  r=fmod(n,10.0);
  //r = fmodf(n, 10.0); thansk to chux
  n /= 10;
  printf("%f %f", n, r );
}

相关内容

最新更新