#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/
或fmodf
http://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 );
}