Why your program throws 'not-responding' error ?
scanf()
reads input from the standard input stream stdin.
int scanf(const char *restrict format, ... );
In your program,
scanf("%f %f", x1, x2);
the 2 '%f
's are the conversion specifications. The input is read and converted to float
. The arguments following "%f %f",
are taken as addresses wherein the converted values are to be stored. Pointers are expected. In your program, two float *
are expected. You are providing x1
, x2
, both are of type float
which is itself is wrong.
x1
and x2
are uninitialized and hence contains garbage values. Considering them as addresses and reading the values at them is invalid memory read, resulting in segmentation fault. Hence, you receive not-responding error.
Solution:
You should pass in proper arguments to scanf()
(Read this);
To store %f
which is of type float
, you need float*
which can hold the address of a float
variable.
&
operator gives you the address of the operand. You can solve your problem by,
scanf("%f %f",&x1,&x2);