当前位置: 首页 > news >正文

为什么printf()用%f输出double型,而scanf却用%lf呢?

之前没有注意过这个问题,  转自: http://book.51cto.com/art/200901/106880.htm

问:有人告诉我不能在printf中使用%lf。为什么printf()用%f输出double型,而scanf却用%lf呢?

答:printf的%f说明符的确既可以输出float型又可以输出double型。 根据"默认参数提升"规则(在printf这样的函数的可变参数列表中 ,不论作用域内有没有原型,都适用这一规则)float型会被提升为double型。因此printf()只会看到双精度数。参见问题15.2。

对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。(通过指针)向float存储和向double存储大不一样,因此,scanf区别%f和%lf。

下表列出了printf和scanf对于各种格式说明符可以接受的参数类型。

 

格式

printf

scanf

%c

int

char *

%d, %i

int

int *

%o, %u, %x

unsigned int

unsigned int *

 

(续)

 

格式

printf

scanf

%ld, %li

long int

long int *

%lo, %lu, %lx

unsinged long int

unsigned long int *

%hd, %hi

int

short int *

%ho, %hu, %hx

unsigned int

unsigned short int *

%e, %f, %g

double

float *

%le, %lf, %lg

n/a

double *

%s

char *

char *

%[...]

n/a

char *

%p

void

void **

%n

int *

int *

%%

none

none


(严格地讲,%lf在printf下是未定义的,但是很多系统可能会接受它。要确保可移植性,就要坚持使用%f。)

另外, 关于才c++标准输入输出同步, 如果是用了ios::sync_with_stdio(false)的话是可以达到近似scanf的速度的。

static void sync_with_stdio();
Synchronizes the C++ streams with the standard I/O system. The first time this function is called, it resets the predefined streams (cincoutcerrclog) to use astdiobuf object rather than a filebufobject. After that, you can mix I/O using these streams with I/O using stdinstdout, and stderr. Expect some performance decrease because there is buffering both in the stream class and in the standard I/O file system.
After the call to sync_with_stdio, the ios::stdio bit is set for all affected predefined stream objects, and cout is set to unit buffered mode.

转载于:https://www.cnblogs.com/Stomach-ache/p/3726444.html

相关文章:

  • 新手搭建PHP环境必备知识:windows下PHP5+APACHE+MYSQL完整配置
  • 经典常忆
  • php生成图片注释
  • 陕西驾驶理论考试
  • C语言回顾一“Hello world”
  • 学会九招不再犯愁!移动硬盘使用中心得体会
  • 系统内置的四种网络测试工具
  • Android: ListView数据的分批加载 以及 Handle 总结
  • 输入gpedit.msc时提示找不到文件这是什么原因
  • JS 公共方法
  • 【Vegas原创】FCKEditor:Times附近有语法错误
  • 设置NumericStepper控件不可用状态的上下箭头颜色。
  • 关于 Linux
  • 革新2410D开发板试用手记(三)
  • SQL Server 2005系列教学(9) SQL 存储过程
  • 【React系列】如何构建React应用程序
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • CAP 一致性协议及应用解析
  • Docker下部署自己的LNMP工作环境
  • gitlab-ci配置详解(一)
  • javascript面向对象之创建对象
  • linux学习笔记
  • Median of Two Sorted Arrays
  • python 装饰器(一)
  • React系列之 Redux 架构模式
  • React组件设计模式(一)
  • vue-cli3搭建项目
  • 基于 Babel 的 npm 包最小化设置
  • 基于遗传算法的优化问题求解
  • 前端性能优化--懒加载和预加载
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 算法之不定期更新(一)(2018-04-12)
  • 小李飞刀:SQL题目刷起来!
  • #### go map 底层结构 ####
  • #《AI中文版》V3 第 1 章 概述
  • $().each和$.each的区别
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (分布式缓存)Redis哨兵
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (图)IntelliTrace Tools 跟踪云端程序
  • (转)【Hibernate总结系列】使用举例
  • (转)菜鸟学数据库(三)——存储过程
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .net Application的目录
  • .Net的C#语言取月份数值对应的MonthName值
  • .net通用权限框架B/S (三)--MODEL层(2)
  • /etc/fstab 只读无法修改的解决办法
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • @ModelAttribute注解使用
  • @RequestMapping 的作用是什么?
  • [20140403]查询是否产生日志
  • [android] 天气app布局练习