The answer is y
is an int
, and can't hold the large output value, so you get signed overflow into the negative number space, which is undefined behavior and should be considered a bug you need to fix. Choose a larger data type for y
, such as long long
, or even better, int64_t
(included with #include <stdint.h>
, or double
, which is the type pow()
naturally returns. See here: https://en.cppreference.com/w/c/numeric/math/pow.
Note that pow()
returns a type double
, which uses exponential notation to represent larger numbers, at the cost of losing precision as the number grows away from zero. To go even larger, use powl()
to do the math with the long double
type, and then make y
of type long double
as well.
This is a related answer that may be considered by some to be a duplicate, since a very similar problem is happening: Pow() calculates wrong?.