scanf is collecting the wrong input

前端 未结 3 1635
花落未央
花落未央 2021-01-29 08:13
 #include 
 int main(void)
 {
      double c;
      scanf(\"%f\", &c);
      printf(\"%f\", c);
 }

This is an exerpt from a program

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-29 08:41

    You need to use "%lf" for double.

    This is the warning from clang compiler.

    warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat] scanf("%f", &c);

    Here is the scanf reference. It's format is %[*][width][length]specifier. The specifier for 'floating point number' is f. So we use %f to read float x. To read double x, we need to specify the length as l. Combined the format is %lf.

提交回复
热议问题