Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use 'y1' as a float variable in C

Tags:

c

Introduction: I'm building a simple program to input the coordinates of two numbers from user and print the distance between those coordinates.

Issue: The problem is that the compiler isn't letting me set y1 as a float variable. If I change y1 to ycord1 or any other variable name, the program works fine.
I'm having no issues setting x1 and z1 as float variables, the problem is only with y1.

Here's the screenshot of the error I'm getting:

screenshot

The Code:

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

float square(float, float);

float x1, y1, z1;
float x2, y2, z2;

float distance = 0;

int main(int argc, const char * argv[]) {

    printf("Enter coordinates of Point 1: ");
    scanf("%f, %f, %f", &x1, &y1, &z1);

    printf("Enter coordinates of Point 2: ");
    scanf("%f, %f, %f", &x2, &y2, &z2);

    distance = sqrtf(square (x2, x1) + square (y2, y1) + square (z2, z1));

    printf("Distance between the 2 points is %.3f\n\n", distance);

    return 0;
}

float square(float a, float b)
{
    float c = a - b;
    c = c * c;
    return c;
}

I've tried 2 different compilers but both are producing the same error.

I tried searching Is y1 a reserved variable in C on Google, but couldn't find any meaningful results. Let me know if I also need to provide a screenshot of the code working when I replace y1 with any other variable.

like image 302
Harshit Jindal Avatar asked Oct 15 '25 23:10

Harshit Jindal


2 Answers

y1 is a Bessel function of the second kind. However, the identifier that you're using is not an identifier reserved by the C standard library, but comes from POSIX.

If you request that the compiler sticks to standard, without extensions, for example with gcc use -std=c11, then <math.h> doesn't declare that identifier. The linker should in theory just fill in those symbols that are yet unresolved. Of course if you link in any library that expects y1 to actually be the Bessel function and not your variable, then you will have wholly undefined behaviour - perhaps you should use static double y1 here.


Then of course, in your case the best thing would be to have y1 as a local variable instead; there the global declaration of y1 wouldn't matter.


y1 returns a bessel function of order 1. It is not reserved, but it is defined in math.h. Even if you don't include math.h, it is defined as a built-in function by the compiler (most probably so people don't use it as global variables.)

You cannot override it as a variable in the same global space. And no one expects a short-named variable as a global variable anyway, since global variables were meant to be used across functions or even multiple source files and modules, so usually are more descriptive.

Since you are only using y1 and all the rest of the global variables inside main() function, define them as local variables of main():

int main(int argc, const char * argv[]) {
    float x1, y1, z1;
    float x2, y2, z2;
    ...
like image 28
Martin Avatar answered Oct 18 '25 16:10

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!