9

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.

hamb
  • 159
  • 1
  • 2
  • 11

5 Answers5

14

For everyone else who seek such an answer:

This will not work:

gcc my_program.c -o my_program

It will produce something like this:

/tmp/cc8li91s.o: In function `main':
my_program.c:(.text+0x2d): undefined reference to `pow'
collect2: ld returned 1 exit status

This will work:

gcc my_program.c -o my_program -lm
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64
  • 2
    This is already stated in the question itself. (And in numerous other Qs and As on Stack Overflow.) – Mat May 27 '12 at 14:00
9

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.)

Mat
  • 202,337
  • 40
  • 393
  • 406
4

You are lacking the printf line to print the value to stdout. Try this one:

#include <stdio.h>
#include <math.h>

int main() {
        double a=4, b=2, c;

        c = pow(b,a);
        printf("%g^%g=%g\n", a,b,c);
        return 0;
}

The output will be:

4^2=16
dAm2K
  • 9,923
  • 5
  • 44
  • 47
4

There is confusion here regarding base and exponent. This is not immediately apparent because both 2^4 and 4^2 equal 16.

void powQuestion()
{
    double a, b, c;

    a = 4.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 4 ^ 2 = 16

    a = 9.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 9 ^ 2 = 512  >> Wrong result; 512 should be 81 <<


    // K & R, Second Edition, Fifty Second Printing, p 251: pow(x,y) x to the y

    double x, y, p;

    x = 9.0;
    y = 2.0;
    p = pow(x, y);

    printf("%g ^ %g = %g\n", x, y, p);      // Output: 9 ^ 2 = 81


    // even more explicitly

    double base, exponent, power;

    base = 9.0;
    exponent = 2.0;
    power = pow(base, exponent);

    printf("%g ^ %g = %g\n", base, exponent, power);    // Output: 9 ^ 2 = 81
}
0

pow() function return value of double data type

so, we have to declare variable of data type double or float where we store pow() value

error in your code

use printf to print value





#include <stdio.h>
#include <math.h>

int main() {
        int a=4, b=2;
        double result;

        result = pow(b,a);
        printf("%d^%d=%g\n", a,b,result);
        return 0;
 } 
  • 1
    There is no error in op's code, it just doesn't do anything. `b = pow(a,b)` correctly calculates `a` to the power of `b` and stores it in `b`, and `b` is a `double` so it's fine. The only thing missing in op's code is a `printf` to create some sort of output – Willis Hershey Oct 24 '22 at 13:07