0

I want work with this function and it will take a variable as argument but is not working with me. If i put the number it works but a number in argument does not work.

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

int main(void)
{
  float x;
  x = 7;
  printf("%f", sqrtf(x)); // produces error "undefined reference to `sqrtf'"
}
int main(void)
{
   printf("%f", sqrtf(7)); // works
}

1 Answers1

3
  1. sqrtf(7) is evaluated compile time and sqrtf function is not called. (it "works")
  2. sqrtf(x) is evaluated run time and sqrtf function is called. (it does not "work" - ie it does not link) enter image description here https://godbolt.org/z/oY6zfqxd1

You need to add -lm compile option to link against the math library. If you do not, the program will not link.

0___________
  • 60,014
  • 4
  • 34
  • 74