-1

I want to do an exponent calculation in C. I tried **, but compiler threw an error.

Is there such operator in C? If not, how to calculate exponent?

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
Steve Wang
  • 27
  • 7

1 Answers1

2

No, there isn't such operator in C. There are functions for this: pow(), powf(), powl() (respectively for double, float and long double) defined in header math.h

First parameter is base, second exponent.

Of course, them being defined for floating point types, doesn't mean you cannot use them for integers.

int x = pow(10, 2);

The result of pow() will be properly cast from 100.0 to 100 and assigned to integer variable x

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
  • 2
    It is typically not a good idea to use floating point arithmetic when working with integers, and you expect exact results. You really have to know what you are doing. `(int) pow(10, 2)` will probably return `100`, but to make sure it doesn't return `99` you have to read and understand all the relevant parts of the C and IEEE 754 standards. In this case, it is much simpler, faster and safer to just do `10 * 10`. – HAL9000 Sep 04 '21 at 11:35