C's pow function refuses to work with variable exponent-Collection of common programming errors

It’s a very interesting behavior, and a good learning example.

To solve your problem, add

-lm

to your gcc command line (provided you’re using gcc). This tells the compiler to link against the math library.

What seems to be going on, is that if you’re using

pow(2.0, 3);

the compiler realizes this expression evaluates to a constant, and does mere substitution.

Thus, no library function has to be called.

Originally posted 2013-11-09 19:03:22.