scanf format warning for double

2020-05-02 11:33发布

问题:

I'm having a small problem with a program I'm working on, I keep getting the warning format '%1f' expects type 'float *' but argument 2 has type 'double *' So I'm fairly sure it's a problem with my scanf format.

I've tried looking all over for a solution but can't seem to find one.

This function reads in two numbers.

void read(double *n1, double *d1)
{
    printf("Enter the number n1: ");
    scanf("%1f", n1);
    printf("Enter the number d1: ");
    scanf("%1f", d1);
}

回答1:

Use scanf("%lf", n1) for double; Note the "l" (el, not "one"). If you are new to programming, try to get familiar with documentation, e.g. cppreference. There you find, for example, the matrix of format and length specifies for scanf.

Have fun with learning programming, use google et al, and don't hesitate to ask :-)



回答2:

You made a typo and then you duplicated it...

scanf("%1f", n1);

Should be written

scanf("%lf", n1);

Note the difference between l (lowercase L) and 1 (number one).

%lf stands for long float, which is not an actual C type, but a way to distinguish float (%f) and double (%lf). The l can be used with d and i to specify long int and u for long unsigned int.

These characters are difficult to distinguish, especially with fixed pitch fonts used for programming, for this very reason, one should avoid naming a variable l, ll etc. and the long integer constant 1l should be written 1L.



回答3:

Use %lf format specifier for double data type.



回答4:

Use correct format specifiers for their respective data types

  • float %f
  • double %lf
  • int %d or %i
  • unsigned int %u
  • char %c
  • char * %s
  • long int %ld
  • long long int %lld