[Error] invalid operands of types ‘int‘ and ‘double‘ to binary ‘operator%‘

在运行下面的代码时,编译器报错[Error] invalid operands of types ‘int’ and ‘double’ to binary ‘operator%’

#include <stdio.h>
#define mod 1e9+7

int main()
{
    
    
	int a=5;
	printf("%d\n",a%mod);
	
	return 0;
}

报错原因:C语言中规定%(取模)运算符的两个操作数必须同为整数类型,而1e9+7被认为是浮点类型。只需将1e9+7改为1000000007,程序就可以正常编译运行。

也可以进行强制类型转换

#define mod (int)(1e9+7)

或者用const int

const int mod=1e9+7;

这两种方法程序也可以通过编译,正常运行。

猜你喜欢

转载自blog.csdn.net/weixin_46155777/article/details/108686492