Search code examples
cgccpowmath.h

pow function in C


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.


Solution

  • 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:

    int main(void) {
      ...
      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.

    #include <stdio.h>
    ...
      printf("%f\n", b);
    ...
    

    You must use a floating point conversion specifier (f, g or e) to print double values. You cannot use d or others and expect consistent output. (This would in fact be undefined behavior.)