Scanf for double not working in Dev C++

后端 未结 4 1484
忘掉有多难
忘掉有多难 2021-01-27 11:51

I am having problem with floating point numbers. I think something is clashing here.

The output is :

\"Scre

相关标签:
4条回答
  • 2021-01-27 12:12

    Using wrong format specifier invoke undefined behavior and that's why you are getting unexpected result. Once UB is invoked, you may either get expected or unexpected result. Nothing can be said.
    Use %lf to read double type data.

    0 讨论(0)
  • 2021-01-27 12:13

    Use

    scanf("%lf", &y);
    

    instead. Since scanf("%f", &y); works for floats only.

    If you enable compiler warnings it would tell you that the format specifier "%f" expects a float * and not double * argument.

    0 讨论(0)
  • 2021-01-27 12:36

    You should use

    scanf("%lf", &y);
    
    0 讨论(0)
  • 2021-01-27 12:38

    You are using the wrong format specifier in the scanf and doing this will result in UB(Undefined Behaviour).The correct format specifier for a double is %lf while that of a float is %f. So Just change your scanf to

    scanf("%lf",&y);
    
    0 讨论(0)
提交回复
热议问题