Why is my power operator (^) not working?

前端 未结 8 1329
半阙折子戏
半阙折子戏 2020-11-22 02:12
#include 

void main(void)
{
    int a;
    int result;
    int sum = 0;
    printf(\"Enter a number: \");
    scanf(\"%d\", &a);
    for( int i =         


        
8条回答
  •  忘了有多久
    2020-11-22 02:57

    Well, first off, the ^ operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.

    Now, regarding your problem with using the pow() function, some googling shows that casting one of the arguments to double helps:

    result = (int) pow((double) a,i);
    

    Note that I also cast the result to int as all pow() overloads return double, not int. I don't have a MS compiler available so I couldn't check the code above, though.

    Since C99, there are also float and long double functions called powf and powl respectively, if that is of any help.

提交回复
热议问题