I enter 5.08c. Output empty

#include <stdio.h> int main() { int x; float x1; char ch; scanf("%f%c", &x1, &ch); if ((x1 == 5.08) && (ch == 'c')) printf("%5.3f \n", x1 / 2.54); //5.08cm = 2" } 

Compare 5.08 and the symbol 'c'. Where is the mistake? Both must give the truth.

  • @marioxxx. To format a code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky
  • okay okay didn't know) - marioxxx

1 answer 1

The problem is that (float) 5.08 is not equal (double) 5.08 oddly enough (for me too).

Write ... (x1 == (float) 5.08) ... and it will work.

The default constants are double. In general, I advise you not to use float without much need to save memory.

  • ok float =% f double =% lf - marioxxx
  • Or this: x1 == 5.08f - skegg
  • In general, it is better to compare fractional numbers with a certain accuracy, for example, if fabs (x1-5.08) <0.0001, then x1 is equal to 5.08 with an accuracy of 4 decimal places. - insolor