If you’re searching for a c power operator, you won’t find one. C++ has no built-in symbol for raising a number to an exponent, so 2 ** 10 will not compile, and 2 ^ 10 gives 8, not 1024. The standard way to raise a number to a power is the std::pow function from the <cmath> header. This article explains why the common symbols don’t work, how to use std::pow, and how to get whole-number results without surprises.
Why ** and ^ do not work
Some languages, such as Python and JavaScript, use ** for exponents. C++ does not. If you write this:
int x = 2 ** 3;
the compiler rejects it. It reads 2 ** 3 as 2 * *3, which tries to dereference the plain number 3. That is not allowed, so the code does not compile.
The caret (^) is not an exponent operator either. It is bitwise XOR, which works on the bits of two integers:
int y = 5 ^ 3; // 101 XOR 011 is 110, so y is 6
To learn how XOR works on individual bits, look into bitwise xor c.
Using std::pow
std::pow takes a base and an exponent and returns the result:
#include <cmath>
#include <iostream>
int main() {
std::cout << "2 to the power of 10 is " << std::pow(2, 10) << "\n";
std::cout << "2.5 squared is " << std::pow(2.5, 2) << "\n";
}
Build and run it with:
g++ -std=c++17 -Wall power.cpp -o power
./power
The output is:
2 to the power of 10 is 1024
2.5 squared is 6.25
A few things to know about std::pow:
- It returns a
double. A double stores fractional values, so the result is a double even when it is a whole number. If you’re not sure what that type holds, read what does double do in c. - It accepts integers. Since C++11,
<cmath>has overloads for integer arguments. They convert the arguments todouble, sostd::pow(2, 10)compiles without a cast. The idea that one name can cover several argument types is covered in does c have function overloading. - In plain C, it lives in
<math.h>. On many systems you also need to link the math library, for examplegcc power.c -o power -lm.
Getting a whole-number result
Because the result is a double, you often want an int next. A plain cast can give a surprising answer. Floating-point math is not always exact, so a result that should be a whole number can come back slightly low, and the cast then drops the fraction. Rounding first avoids this:
int result = static_cast<int>(std::round(std::pow(3, 4))); // 81
std::round is also in <cmath>. It returns the nearest whole number as a double, which the cast then turns into an int.
Be careful with size. An int often holds values only up to about 2.1 billion. Converting a larger double to an int has undefined behavior. For the usual ranges of each type, see primitive data types in c.
Exact integer powers with a loop
When you only need whole numbers, a loop avoids floating-point rounding entirely. This version multiplies the base by itself exponent times:
#include <iostream>
int main() {
long long base = 2;
int exponent = 10;
long long result = 1;
for (int i = 0; i < exponent; ++i) {
result *= base;
}
std::cout << base << " to the power of " << exponent
<< " is " << result << "\n";
}
The output is 2 to the power of 10 is 1024.
Keep two limits in mind:
- The loop handles only non-negative whole exponents. For a negative or fractional exponent, use
std::pow. - The result must fit in the type. Signed integer overflow is undefined behavior, so a result that is too large is a bug, not just a wrong number.
Powers of two with shifts
For powers of two, the left shift operator gives the same answer as repeated doubling:
int n = 5;
int value = 1 << n; // 32, which is 2 to the power of 5
This works for small non-negative values of n that fit in the type. Shifting by a negative amount, or by at least as many bits as the type holds, is undefined behavior. The shift operator in c explains these rules in more detail.
Can you define your own power operator?
C++ lets you overload existing operators for your own types, which is covered in operator overloading in c. That does not solve the ** problem. C++ has no ** token, and you cannot invent new operator symbols.
You could overload ^ for your own class, but that changes what the symbol means for that type, and readers who know XOR will be surprised. A named function, such as power(base, exponent), is clearer.
Common mistakes
- Forgetting
#include <cmath>. The compiler reports thatpoworstd::powis not declared. - Using
^expecting a power. You get bitwise XOR instead, which is usually a different number and not an error. - Expecting an
intback.std::powreturns adouble, so round and cast it when you need anint. - Missing the math library in C. On some systems, linking fails with an “undefined reference to pow” error until you add
-lm.
Summary
- C++ has no power operator.
**is not an operator in C++, and^is XOR. - Use
std::powfrom<cmath>for general exponents. It returns adouble. - Round with
std::roundbefore converting toint. - Use a loop for exact integer powers with non-negative exponents.
- For powers of two,
1 << nis a simple option.