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

Re: sqrt C function(clarification)



Shaul Karl 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).


You could still have told us what your command looked like but with answers you are getting you may not need to now.

None of the many variations of code that I tried gave the above error.

Were you thinking C or C++? A slightly relevant point is that the optimizer removes the sqrt line in every case below since you are not using the result of the sqrt.

If you are thinking C then the casting you are doing should be (double) or (float) not just double or float. That one change gets rid of all the errors from the following.

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);
}

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);
}

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);
}


[12:13:02 tmp]$ cat sqrt.c
#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);
}
[12:13:10 tmp]$ gcc -Wall sqrt.c -o sqrt -lm
sqrt.c: In function `main':
sqrt.c:9: parse error before `double'
[12:13:25 tmp]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20010319 (Debian prerelease)
[12:21:32 tmp]$ You can not have a deceleration (double sqrt(double);) after a command (num = 16;) in the same block. Replacing the order of
    num = 16
and double sqrt(double num);


This is another way of saying what I said about casting above. In C++ declarations can occur anywhere but in C they must precede all of the executable statements.

As a last point Craig was correct about the optimizer - at least for gcc.

  float x = 5;

does indeed convert the integer 5 to a float 5 at compile time.

Paul Scott





Reply to: