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

int main(void) 
{ 
    double x = 4.0, result; 

    result = sqrt(x); 
    printf("The square root of %lf is %lfn", x, result); 
    return 0; 
} 

This code does not work because it is taking the square root of a variable. If you change the sqrt(x), to sqrt(20.0), the code works just fine, why? Please explain.

Also, how do I get the square root of the variable (which is what I really need)?

OUTPUT:

matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c
/tmp/ccw2dVdc.o: In function `main':
sqroot2.c:(.text+0x29): undefined reference to `sqrt'
collect2: ld returned 1 exit status
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

NOTE:sqroot1 is the sqroot of 20.0. sqroot2 is the sqroot of a variable.

matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c -lm
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot2
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

SOLVED.

Starkey
  • 9,673
  • 6
  • 31
  • 51
matthewmpp
  • 149
  • 3
  • 3
  • 5
  • 2
    Post how it doesn't work, since it would work... – Yann Ramin Nov 08 '10 at 01:05
  • That code looks fine to me. What result are you getting when you run it? – Firoze Lafeer Nov 08 '10 at 01:06
  • You say it doesn't work: what do you see? It should work, although as a matter of style I'd put the result variable on a different line. Also you're missing a / before the final n in your printf. – winwaed Nov 08 '10 at 01:06
  • how come it doesn't work? prints "The square root of 4.000000 is 2.000000n" for me (http://codepad.org/Mv4NNVFU) – DennyRolling Nov 08 '10 at 01:06
  • Please format your console sample to be half decent. Thank you and good luck! – Moshe Nov 08 '10 at 01:56
  • Accept Kizaru's answer as correct since (s)he did such a fabulous job of guessing your problem. – Starkey Nov 08 '10 at 02:11
  • possible duplicate of [sqrt() function not working with variable arguments](http://stackoverflow.com/questions/3533594/sqrt-function-not-working-with-variable-arguments) – Stephen Canon Nov 08 '10 at 02:31
  • As an aside, the `l` in `%lf` is ignored - `%f` is also a correct format specifier for `double` arguments. – caf Nov 08 '10 at 04:42
  • This code is working fine for me. I am able to get the proper sqrt. I think u need to revise on ur IDE. – EnthuDeveloper Oct 21 '11 at 06:54

2 Answers2

36

The code should work just fine if you are linking in the proper libraries (libc.a and libm.a). Your issue is probably that you are using gcc and you are forgetting to link in libm.a via -lm, which is giving you an undefined reference to sqrt. GCC calculates the sqrt(20.0) at compile time because it is a constant.

Try to compile it with

gcc myfile.c -lm

EDIT: Some more information. You can confirm this by looking at the generated assembly when you replace x with a constant in the sqrt call.

gcc myfile.c -S

Then take a look at the assembly in myfile.s and you will not see the line call sqrt anywhere.

Kizaru
  • 2,443
  • 3
  • 24
  • 39
  • 4
    If you made the same mistake and came to the same conclusion in your introductory programming course, I'm sure you'd be Miss Cleo too. – Kizaru Nov 08 '10 at 01:20
  • @qed It's not a gcc command or option. It specifies gcc to link in libm.a (or whatever naming convention is used). For more info: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html – Kizaru Aug 15 '13 at 19:36
1

You should do it like this:

root@bt:~/Desktop# gcc -lm sqrt.c -o sqrt
root@bt:~/Desktop# ./sqrt
The square root of 4.000000 is 2.000000n
root@bt:~/Desktop# 
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43