Is the ^ operator available in the C language? I have tried using it but it gives a faulty output. Does it denote raising an integer to the power of something
Asked
Active
Viewed 95 times
-5
-
Read [this C reference website](https://en.cppreference.com/w/c) then a C standard like [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). Study for inspiration the source code of existing open source software like [GNU make](https://www.gnu.org/software/make/) – Basile Starynkevitch Mar 30 '21 at 08:26
-
1C doesn't have a power operator. It has the function `pow()`. – Barmar Mar 30 '21 at 08:26
-
Does this answer your question? [Why is my power operator (^) not working?](https://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working) – Damien Mar 30 '21 at 08:44
-
Why was this closed as a _tool recommendation_? – Lundin Mar 30 '21 at 10:28
1 Answers
4
It works just fine and means bitwise XOR. That is, 1^2
gives 3.
Unfortunately C doesn't provide a function to take power of integers. This is a known flaw of the language. You have to roll out such a function yourself either by using multiplication in a loop, or use the slow floating point function pow
.

Lundin
- 195,001
- 40
- 254
- 396
-
Kinda ironic that a language so rich in operators doesn't have an exponentiation operator, but C was originally designed for systems programming, not numerical work, and its operator set reflects that. – John Bode Mar 30 '21 at 17:15
-
@JohnBode What truly baffles me is that none of the standard committees from 1989 to 20xx have added a function `powi`/`intpow` etc to the standard libs, so that we don't have to invent that wheel over and over. – Lundin Mar 31 '21 at 06:13