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

void main()
{
    int i, diff, sum = 0, num1 = 6, num2 = 2;

    for(i = 0; i <= 4; i++)
    {
        diff = num1 - num2;
        sum += pow(diff, i);
    }

    printf("%d", sum);
}

Whenever I am trying to execute this program, an error message just pops up saying:

In function main:
undefined reference to pow.

What am I missing here?

Hare Kumar
  • 699
  • 7
  • 23

4 Answers4

12

This is a linker failure. You need to link with the math library, specify -lm at the end of your compiler command. From man pow:

Link with -lm.

hmjd
  • 120,187
  • 20
  • 207
  • 252
6

Math library is not part of libc. You need to link it:

gcc file.c -lm -o file
P.P
  • 117,907
  • 20
  • 175
  • 238
0

The function pow() is defined in the math library. Your compiler by default is not linking your program with that library as a result of which the linker is unable the resolve the call to pow().

To fix this you need to add -lm at the end of your compile/link line which links the math lib to your program.

codaddict
  • 445,704
  • 82
  • 492
  • 529
0

I just ran the same program on my Turbo C and i got output as 341. Check whether your header files are in place in /include folder of your Turbo C folder. Make sure to check whether it contains a Math.h file.