I get a blinking cursor instead of output when running this C program in Ubuntu [closed]
Here's my code for finding the distance between two points. My assignment makes it mandatory to make use of functions, hence a separate one for finding the distance. I don't get any syntax errors but only a blinking cursor when I execute the code. I'm a beginner in C.
My code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float distance(float *x1, float *y1, float *x2, float *y2);
int main()
{ printf("Input coordinates as x1,y1,x2,y2:\n");
float x1,y1,x2,y2;
scanf("%f %f %f %f ",&x1,&y1,&x2,&y2);
printf("Coordinates are : (%f,%f) and (%f,%f)\n",x1,y1,x2,y2);
printf("%f",distance (&x1,&y1,&x2,&y2));
return 0;
}
float distance(float *x1, float *y1, float *x2, float *y2)
{ float d= sqrt(pow((*x1-*x2),2)+pow((*y1-*y2),2));
printf("%f", d);
return d;
}while compiling: gcc -o 1 1.c -lm output:
Input coordinates as x1,y1,x2,y2:
1 2 3 4That's it. The cursor keeps blinking without providing any output.
Also, I tried putting in print statements at various points. Any print after the scanf doesn't work.
2 Answers
The question you are asking is not specific to Ubuntu but to C programming. I would suggest you ask such questions at Stackoverflow -- it's for your own benefit, as well, since you'll reach a different audience.
Anyway, you should read up on the scanf () function as it is trying to match what you're typing in on the keyboard. That includes any spaces, newlines, etc. that you have in the formatting string. Then, you also need to end your input into standard in with the "Return key".
For your problem, change:
scanf("%f %f %f %f ",&x1,&y1,&x2,&y2);to:
scanf("%f %f %f %f",&x1,&y1,&x2,&y2);(Remove the space at the end of the formatting string.) And then it should work as expected.
Personally, I've always used scanf to read a value at a time. As shown in the example here. I admit that this was just my way of avoiding the problem you're seeing.
1Two things:
gccis for compiling, not running code.-oflag specifies the name of new file to create. Because you run gcc and there's no errors, it means it compiled the C code1.cinto a file1that you specified with-oflag. Thus, you can just run./1in terminal.
Note that using numbers for executable names is bad practice and also bad naming convention. Use gcc -o distance -lm 1.c instead, and run ./distance in terminal
Note also that because we give bare name of the file, it will create executable in current working directory. That's why you need ./