#include <stdio.h>
void main(void)
{
int a;
int result;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &a);
for( int i = 1; i <= 4; i++ )
{
result = a ^ i;
sum += result;
}
printf("%d\n", sum);
}
为什么 ^
不能作为电力运营商工作?
原文由 Abid Ali 发布,翻译遵循 CC BY-SA 4.0 许可协议
好吧,首先,C/C++ 中的
^
运算符是按位异或。这与权力无关。现在,关于您使用
pow()
函数的问题, 一些谷歌搜索 显示将参数之一转换为 double 有助于:请注意,我还将结果转换为
int
因为所有pow()
重载返回双倍,而不是int
。我没有可用的 MS 编译器,所以我无法检查上面的代码。Since C99, there are also
float
andlong double
functions calledpowf
andpowl
respectively , if that is of any help.