# [C programming] Calculate distance between two points

Discussion in 'Mac Programming' started by gotenks05, Sep 18, 2009.

1. ### gotenks05 macrumors member

Joined:
Jan 1, 2009
#1
Well, after doing Java programming for about a year, as a hobby. I decided to try and get a grasp on C. Right now, I am trying to make a program with a custom function that finds the distance between two points on a 3D graph and got it written correctly, as far as I know, but GCC gives me an error.

the error is:

However, I look at the code and there is nothing that changes the datatype of that variable and it is only declared once. What am I doing wrong?

the source code looks like this:

Code:
```#include <stdio.h>
#include <math.h>

float x1, x2, y1;
float y2, z1, z2;
float dist3d (float ux, float uy, float uz, float vx, float vy, float vz);

int main()
{
printf("Enter the first point in form of (x, y, z) without commas or parenthesis:  ");
scanf ("%f %f %f", &x1, &y1, &z1);

printf("Enter the second point in form of (x, y, z) without commas or parenthesis:  ");
scanf ("%f %f %f", &x2, &y2, &z2);

printf("The distance between (%f, %f, %f) and (%f, %f, %f) is:  %f\n", x1, y1, z1, x2, y2, z2, dist3d(x1, y1, z1, x2, y2, z2));

return 0;
} /* end of program */

float dist3d(float ux, float uy, float uz, float vx, float vy, float vz)
{
return sqrt(pow((vx - ux), 2) + pow((vy - uy), 2) + pow((vz - uz), 2));
}```

2. ### mslide macrumors 6502a

Joined:
Sep 17, 2007
#2
You're getting that error because 'y1' is also a function in the math library. It is for bessel functions (along with y0, and several others).

I would make x1/2, y1/2, z1/2 local variables inside main and not use global variables. That would eliminate your error. Besides, as your program stands right now, they do not need to be global anyway.

3. ### autorelease macrumors regular

Joined:
Oct 13, 2008
Location:
Achewood, CA
#3
Edit: Beat by 2 minutes. Damn.

Believe it or not, y1() is the name of a library function. (It computes the Bessel function of the second kind and order 1.)

Thus, you can't declare another global variable called y1.

You should move the lines

Code:
```float x1, x2, y1;
float y2, z1, z2;```
into main().

4. ### kpua macrumors 6502

Joined:
Jul 25, 2006
#4
Here's where the flat C namespace can bite you.

Try 'man y1' on the command line, and you'll see it's already defined in math.h to be a bessel function (along with j0, j1, jn, y0, and yn). That means, you aren't able to declare a variable with the same name in the global scope, just like you wouldn't be able to name a global variable 'main'.

But really, there's no reason for those variables to be in the global scope in the first place. Just move them into the main function, and you'll be fine.

EDIT: Not fast enough... I guess 3rd time's a charm, yes?

5. ### gnasher729 macrumors P6

Joined:
Nov 25, 2005
#5
As a rule, you should only use "float" instead of "double" if you have a very good reason to do do. If you don't know which one to prefer, use "double". It will save you trouble.

6. ### autorelease macrumors regular

Joined:
Oct 13, 2008
Location:
Achewood, CA
#6
True. Usually floats are only used if you're doing work with graphics or really need to save space. As an approximate rule of thumb, floats are accurate to about 6 decimal places, and doubles are accurate to about 15. Keep that in mind.