我正在编写一个程序,并且已经在这部分工作了很长一段时间.
int y;
y = 111 % pow(10,2);
printf("%d",y);
Run Code Online (Sandbox Code Playgroud)
显示的错误是invalid operands of types 'int' and 'double' to binary 'operator%'.
有没有解决方法(即使用该pow()功能)?
在C中,无论输入参数的类型如何,都pow返回double.
并且a double不能用作参数%.
因此编译器发出错误.
解决方案是使用10 * 10而不是使用pow将数字提升到其第二功率.请注意,因为它%具有与*您需要将表达式编写为相同的优先级111 % (10 * 10).