C语言使用printf(“%lf“)输出double类型显示-0.000000问题,CodeBlocks

问题描述

#include<stdio.h>
#include<math.h>
int main()
{
	double a,b,c;
	scanf("%lf %lf %lf",&a,&b,&c);
	double x=(a+b+c)/2,s;
	printf("%f\n",x);
	double p=x*(x-a)*(x-b)*(x-c);
	printf("%f\n",p);
    s=sqrt(p);
	printf("%lf",s);
    return 0;
}

运行以上代码,输入3 4 5,输出为

6.000000
36.000000
-0.000000

因此提出问题:为什么在printf()中使用"%f"就行但是"%lf"就不行呢?

问题的解释

C标准的问题

C99的 文档 中有如下的说明:

%lf conversion specifier allowed in printf1.

l (ell): Specifies that (…) has no effect on a following a, A, e, E, f, F, g, or G conversion specifier1.

L: Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument1.

意思就是之前的标准中,printf()是不接受%lf的,在C99中才接受%lf的使用。具体在 devdocs-printf 中也可以看到。

那为什么%f可以表示float和double类型呢?

在解决这个问题之前,先看一下一些资料,对接下来用到的名词有个概念:

Function prototype,函数原型:

A function prototype is a declaration of a function that declares the
types of its parameters1.

[翻译:函数原型是声明其参数类型的函数的声明。]

Integer promotions,整型提升:

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions1.

[翻译:如果 int 可以表示原始类型的所有值,则该值将转换为 int; 否则,它将转换为unsigned int。这些称为整型提升(可变参数里的char类型参数会被转换成int类型)。所有其他类型都因整型提升而保持不变。]

Default argument promotions,默认参数提升:

If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double1.

[翻译:如果表示被调用函数的表达式的类型不包含原型,则对每个参数执行整数提升,并且浮点类型的参数提升为双精度。]

关于Default argument promotions,默认参数提升更具体的解释:

Parameters to functions without prototype and variadic parameters are subject to default argument promotions2.

[翻译:没有原型和可变参数的函数的参数受默认参数提升的约束。]

If you define a function with a prototype and use it without the prototype or vise versa and it has parameters of type char, short or float, you’ll probably have a problem at run-time2.

[翻译:如果您使用原型定义函数并在没有原型的情况下使用它,反之亦然,并且它具有 char、short 或 float 类型的参数,您可能会在运行时遇到问题。 ]

You’ll have the same kind of problems with variadic functions if the promoted type doesn’t match what is used when reading the argument list2.

[翻译:如果提升的类型与读取参数列表时使用的类型不匹配,您将遇到与可变参数函数相同的问题。]

when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there’s no prototype or when the argument is variadic2.

[翻译:至于默认提升何时生效:默认参数提升恰好在参数的预期类型未知时使用,也就是说,当没有原型或参数是可变参数时。]

图一图二是来自 stackoverflow: Default argument promotions in C function calls 的例子,可以帮助我们更好的理解上面的几段话。

图一2
图一
图二2
图二

总结/回答问题:为什么%f可以表示float和double类型?

我个人的理解是:当函数的参数没有原型时,或者是可变参数函数,不论作用域内有没有原型,都会有一个参数提升的过程。而printf()在 文档 中为:

#include <stdio.h>
int printf(const char * restrict format, ...);

表示printf()是一个可变参数函数,也会遵守这个参数提升的规则,即float类型参数始终提升为double类型,即在prinft()中使用%f即可,此说明符可以输出float和double类型。这就是为什么%f可以表示float和double类型。具体的解释可以看34

在实例中证实上述结果

在CodeBlocks上试了一下如下代码:

#include<stdio.h>
#include<math.h>
int main()
{
	double a=2.3,b=3.4,c=2.3;
	printf("%f\n",a);
	printf("%f\n",b);
	printf("%lf",c);
    return 0;
}

看图三,如果勾选了c99的话,上面代码的结果就是:

2.300000
3.400000
-0.000000

如果没有勾选的话,上面代码的结果就是:

2.300000
3.400000
2.300000

图三:
图三

但是我不理解的是,明明c99标准里面有如下说明,但是在图三中勾选了-std=c99,在printf()中使用%lf还是不行…

printf()和scanf()的不同

Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you’re passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you’re passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double (and, for what it’s worth, for a long double, you use %Lf for either printf or scanf)5

[翻译:请注意,这是 printf 格式字符串与 scanf(和 fscanf 等)格式字符串有很大不同的地方。 对于输出,您正在传递一个值,当作为可变参数传递时,该值将从浮点数提升为双精度数。 对于输入,您传递的是未提升的指针,因此您必须告诉 scanf 是要读取浮点数还是双精度数,因此对于 scanf, %f 表示您要读取浮点数, %lf 表示您要 读取双精度(并且,对于它的价值,对于长双精度,您可以将 %Lf 用于 printf 或 scanf)。C 语言的旧(C99 之前)版本不支持 printf 中的 %lf 格式,这在 printf 和 scanf 中的 double 格式说明符之间造成了表面上的“不一致”。这种表面上的不一致已在 C99 中得到修复。]


  1. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  2. https://stackoverflow.com/questions/1255775/default-argument-promotions-in-c-function-calls ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  3. https://blog.csdn.net/sinat_14958547/article/details/37877405 ↩︎

  4. https://blog.csdn.net/astrotycoon/article/details/8284501 ↩︎

  5. https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf ↩︎

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值