this one prints 100:
int j=2;
int i= pow(10,2);
printf("%d\n", i);
and this one prints 99:
int j=2;
int i= pow(10,j);
printf("%d\n", i);
why?
What's going on is that you have a C implementation whose standard library has a very low quality implementation of pow
which is returning inexact results even when the exact result is representable in the type (double
). The call to pow(10,2)
seems to producing the value just below 100.0
, which, when rounded to an integer, yields 99. The reason you don't see this when the arguments are constant is that the compiler took the liberty to optimize out the call alltogether and replace it with a constant 100 at compiletime.
If your intent is to do integer powers, don't use the pow
function. Write a proper integer power function, or when the exponent is known, just write out the multiplication directly.