[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: sqrt C function(clarification)



On Sat, Aug 11, 2001 at 03:44:17AM -0400, R1nso13@aol.com wrote:
> I'm sorry not to have provided more information in my first e-mail although 
> i'm very glad so many people responded to help, but i've done some more 
> testing:
> 
> specifically i'm getting 'prase error in tmp/x' where x is a long string of 
> characters that tends to change on every run of the complier (at least when 
> i'm compiling other programs). I've tried the following and gotten no errors
> 
> #include <stdio.h>
> #include <math.h>
> 
> double num=16.0;
> 
> int main()
> { 
>      double sqrt(double num);
> return(0);
> }

'double sqrt(double num);' is a declaration, not a function call, so
this doesn't compile to anything very useful. You probably want
something like 'num = sqrt(num);' instead.

> the following warns me that 'function sqrt does not match global variable' 
> but still compiles
> 
> #include <stdio.h>
> #include <math.h>
> 
> float num;
> 
> int main()
> { 
>      float sqrt(float num);
> return(0);
> }

That's because sqrt() is already declared in <math.h>, and you're trying
to redeclare it as something different.

> and this one just doesn't work at all giving the parse error previously 
> described:
> 
> #include <stdio.h>
> #include <math.h>
> 
> double num;
> 
> int main()
> { 
>       num = 16; /*i've also tried 16. and 16.0 here*/
>       double sqrt(double num);
> return(0);
> }

That's because declarations can't go after function calls. Again, you
almost certainly don't want it to be a declaration anyway.

> I'm sorry I was not more clear with my first e-mail and hope this points out 
> the problem better.  Also, how do I let gcc know that I want math.h to be an 
> available library so i dont' have to use the -l option every time if run it?

You can't. Try using a makefile instead. Here's an example (the tab at
the beginning of the second line must be a real tab:

foo: foo.c
	$(CC) $< -o $@ -lm

$(CC) is the C compiler (cc by default, which is a link to gcc on most
GNU systems), $< is the current source file (i.e. foo.c), and $@ is the
current target file (i.e. foo). The whole thing, put in a file called
'Makefile', lets you type 'make foo' and have it work.

Cheers,

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: