0

Why won't code work? After compiling I write down first number to scan and after pressing enter it gives me 'not responding' message in windows.

#include <stdio.h>

int main (void) {
 float x1, x2, y;
 scanf("%f %f", x1, x2);
 if (x1 < x2) {
    y = x1 / x2;
 } else if (x1 == x2) {
    y = x1 * x1 - 3;
 } else {
    y = 4 * x1 + 3 * x2;
 }
 printf("y = %f", y);
 return 0;

}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user2627736
  • 281
  • 1
  • 4
  • 13
  • 5
    `scanf` requires that you pass the *addresses* of the values being read in. That's necessary because C is pass by value. So `scanf("%f %f", &x1, &x2);` – lurker Oct 18 '13 at 18:12
  • Once you fix that, you'll also want to be careful with your multiplications: `x1` and `x2` are floats, but if you multiply them by 4 and 3, the result will be ints. Make sure you use (e.g.) 4.0 and 3.0 in your calculations. – Matt Patenaude Oct 18 '13 at 18:14
  • 2
    @MattPatenaude: No. For arithmetic operators, if one of the operand is `float` and the other is `int`. Resulting data type is `float`. This is called _usual arithmetic conversion_. – smRaj Oct 18 '13 at 18:25
  • 1
    You may want to check that `x2 != 0` before doing `y = x1 / x2;`, too. – Crowman Oct 18 '13 at 18:43
  • @smRaj I stand corrected! Thanks! – Matt Patenaude Oct 18 '13 at 18:45

3 Answers3

1

to make your code working try to enter two numbers with space between them and only then press enter

update: also, you have to pass pointers to scanf, prefix x1 and x2 with &

also, check about floating point numbers equality

Community
  • 1
  • 1
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Not sure why this has been downvoted. Since the question says "after compiling I write down first number to scan and after pressing enter it gives me 'not responding' message in windows", this actually seems to be the most relevant answer. – Crowman Oct 18 '13 at 19:02
1

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);
smRaj
  • 1,246
  • 1
  • 9
  • 13
0

See IIya's answer. In addition:

scanf("%f %f", x1, x2);

You need to provide an address to a variable. So...

scanf("%f %f", &x1, &x2);
Inisheer
  • 20,376
  • 9
  • 50
  • 82