900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 解决invalid operands of types ‘float‘ and ‘int‘ to binary ‘operator %

解决invalid operands of types ‘float‘ and ‘int‘ to binary ‘operator %

时间:2021-09-01 05:22:01

相关推荐

解决invalid operands of types ‘float‘ and ‘int‘ to binary ‘operator %

c语言编程出现错误“invalid operands of types 'float' and 'int' to binary 'operator %”

这是因为求余运算符%要求左边的数是整数,但是把一个float类型的数放在%左边进行运算,就会报以上错误。下面看具体例子:

#include <stdio.h>int main (){int s;float f,x,s,a,y;x=2.5,y=4.7,a=7;s= (int)(x+y);f=x+(a%3)*(s%2)/4;printf("f=%f\n",f); return 0;}

以上代码中a被定义为float类型,第7行就会报错标题中的错误。修改正确也很简单,就是把a定义为int类型:

#include <stdio.h>int main (){int a=7,s;float f,x,y;x=2.5,y=4.7;s= (int)(x+y);f=x+(a%3)*(s%2)/4;printf("f=%f\n",f); return 0;}

谢谢各位看完文章,有疑问或者补充或者建议,可站内联系我。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。