简体   繁体   English

scanf格式警告为双精度

[英]scanf format warning for double

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. 我正在使用的程序有一个小问题,我一直收到警告format '%1f' expects type 'float *' but argument 2 has type 'double *'所以我很确定这是一个问题与我的scanf格式。

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);
}

Use scanf("%lf", n1) for double ; scanf("%lf", n1)用作double Note the "l" (el, not "one"). 请注意“ l”(el,而不是“一个”)。 If you are new to programming, try to get familiar with documentation, eg cppreference . 如果您不熟悉编程,请尝试熟悉文档,例如cppreference There you find, for example, the matrix of format and length specifies for scanf . 例如,您可以在其中找到scanf指定的格式和长度矩阵。

Have fun with learning programming, use google et al, and don't hesitate to ask :-) 玩得开心,学习编程,使用google等,不要犹豫,问:-)

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). 注意l (小写字母L)和1 (数字1)之间的差异。

%lf stands for long float, which is not an actual C type, but a way to distinguish float ( %f ) and double ( %lf ). %lf代表long float,这不是实际的C类型,而是区分float%f )和double%lf )的一种方式。 The l can be used with d and i to specify long int and u for long unsigned int . l可以与di一起使用,以指定long intu用于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 . 这些字符很难区分,特别是使用固定间距的字体进行编程时,由于这个原因,应避免命名变量lll等,而long整数常量1l应写为1L

%lf格式说明符用于双%lf数据类型。

Use correct format specifiers for their respective data types 为它们各自的数据类型使用正确的格式说明符

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM