IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言使用printf(“%lf“)输出double类型显示-0.000000问题CodeBlocks -> 正文阅读

[C++知识库]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 ??

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:22:12  更:2022-10-08 20:26:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 9:09:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码