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 4

That'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.

closed as off-topic by fkraiem, karel, Zanna, pomsky, N0rbert Oct 22 at 6:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – fkraiem, karel, Zanna, pomsky, N0rbert
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Did you press enter after writing 1 2 3 4? – pqnet Oct 22 at 5:45
  • Yes I did. Simply goes to a new line – Sayantan Mukherjee Oct 22 at 5:50

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.

  • 1
    Another better idea would be to use argv positional parameters instead of scanf altogether. But that's another topic entirely. – Sergiy Kolodyazhnyy Oct 22 at 4:23
  • Thanks, solved my problem. – Sayantan Mukherjee Oct 22 at 5:50

Two things:

  1. gcc is for compiling, not running code.
  2. -o flag specifies the name of new file to create. Because you run gcc and there's no errors, it means it compiled the C code 1.c into a file 1 that you specified with -o flag. Thus, you can just run ./1 in 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 ./

Not the answer you're looking for? Browse other questions tagged or ask your own question.