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
  • 131,985
  • 21
  • 239
  • 326
Energy ATP
  • 11
  • 1
  • 1
  • Purely pogramming question, not a compiling question. You'll be better of on SO. – Jacob Vlijm Sep 27 '19 at 23:04
  • 1
    Likely the program doesn't show anything because it is waiting for input - you would need to type something like `4 5` followed by `Enter` and then `Ctrl+D` to indicate end-of-input. – steeldriver Sep 28 '19 at 03:40
  • 2
    We have a duplicate for this already [How to compile a C program that uses math.h ?](https://askubuntu.com/q/332884/295286) – Sergiy Kolodyazhnyy Sep 28 '19 at 06:18
  • 2
    Possible duplicate of [How to compile a C program that uses math.h?](https://askubuntu.com/questions/332884/how-to-compile-a-c-program-that-uses-math-h) – Sergiy Kolodyazhnyy Sep 28 '19 at 06:18
  • Also, after compilation you've created `test` executable in current working directory. You'd need to run it to make it work. But as others mentioned, don't call it `test` , since there already exists standard executable in Linux with same name. Do something like `gcc test.c -o ./my_test -lm` and then run `./my_test` in terminal or via `:!./my_test` in vim, since it's seems like what you're doing – Sergiy Kolodyazhnyy Sep 28 '19 at 06:22
  • @SergiyKolodyazhnyy as mentioned, not a compiling issue, the code has multiple issues. – Jacob Vlijm Sep 28 '19 at 07:28
  • @JacobVlijm What issues specifically ? Aside from line numbers in code itself, which I think OP just copied from vim, not part of the code I hope – Sergiy Kolodyazhnyy Sep 28 '19 at 16:23
  • @SergiyKolodyazhnyy datatypes for one thing. `sqrt()` is double, not int. Then `printf("%d", c);` is incorrect, since c is not int anyway, and (no c programmer, but assuming vala logic is similar), c is an incorrect addition of different types. The test: just compile this way, but commenting out the suspected lines, setting `c = sqrt(30)` works just fine. – Jacob Vlijm Sep 28 '19 at 16:53
  • @Jacob Yeah, that's a good point about sqrt, though can be easily cast to double. As for why there's no error, it's likely because there's no loss of precision with 30. Likely would be a warning with different number. Ok, good job – Sergiy Kolodyazhnyy Sep 28 '19 at 17:17
  • @SergiyKolodyazhnyy you're also right, just tried, to find out c accepts casting this way, To my surprise I must say. Still running a valid sqrt() gives a valid output here, so I assume it is a coding issue rather then a compiling one. – Jacob Vlijm Sep 28 '19 at 17:21

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

Jeffrey Ross
  • 666
  • 4
  • 12
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
  • 35,099
  • 19
  • 57
  • 93
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);
SurvivalMachine
  • 2,773
  • 6
  • 20
  • 32