I write a C code that have power function that is from math.h library. when I compiled my program, I received an error which is " undefined reference to 'pow' function ", I compile my program using gcc compiler (fedora 9).
I insert -lm flag to gcc then, the error is omitted but the output of the pow function is 0.
#include<math.h>
main()
{
double a = 4, b = 2;
b = pow(b,a);
}
Can anyone help me? Is there is a problem in my compiler??
Thanks.
There is confusion here regarding base and exponent. This is not immediately apparent because both 2^4 and 4^2 equal 16.
Your program doesn't output anything.
The 0 you are referring to is probably the exit code, which will be 0 if you don't explicitly return from
main
.Try changing it to a standards-compliant signature and return
b
:Note that the return values is essentially limited to 8 bits-worth of information, so very, very limited.
Use
printf
to display the value.You must use a floating point conversion specifier (
f
,g
ore
) to printdouble
values. You cannot used
or others and expect consistent output. (This would in fact be undefined behavior.)You are lacking the printf line to print the value to stdout. Try this one:
The output will be:
For everyone else who seek such an answer:
This will not work:
It will produce something like this:
This will work: