0

why i can't run the program with sqrt (a)

code :

    #include <stdio.h>                                                                               
  1 #include <math.h>
  2 int main ()
  3 {         
  4     int a, b, c;
  5     scanf("%d %d ", &a, &b);
  6     c = sqrt(a) + b;
  7     printf("%d", c);
  8     return 0;
  9 }

error :

/usr/bin/ld: /tmp/ccfUQsrW.o: in function `main':
test.c:(.text+0x42): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

if I compile the program

:!gcc test.c -o ./test -lm

then the program does not show anything

steeldriver
  • 142,475

3 Answers3

1

Do not call your program "test" since there is already a Linux "test" command and it will likely be invoked in preference to your compiled program. If running "test a =" results in a complaint about a unary operator, you are running the built-in "test". Rename your "test" as "sqrt" and try running "./sqrt".

0

math.h is in the libc6-dev package, make sure that it's installed.

walt@bat:~(0)$ dpkg -S /usr/include/math.h 
libc6-dev:amd64: /usr/include/math.h
waltinator
  • 37,856
0

The program doesn't display anything because you are not flushing output. To do so, replace this line:

printf("%d", c);

with

printf("%d\n", c);