为什么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 (cin, cout, cerr, clog) to use astdiobuf object rather than a filebufobject. After that, you can mix I/O using these streams with I/O using stdin, stdout, 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.
为什么printf()用%f输出double型,而scanf却用%lf呢?的更多相关文章
- 为什么printf()用%f输出double型,而scanf却用%lf呢?
转:https://blog.csdn.net/bat67/article/details/52056057 示例:double x:scanf(“%f”,&x):输入“123.4”,输出x的 ...
- double型(双精度型)的打印(printf) 和scanf
double型,printf()用%f输出,而scanf用%lf来接受输入. 格式 printf scanf %c int char * %d, %i int int * %o, %u, %x u ...
- 只使用处理I/O的printDigit方法,编写一种方法一输出任意的double型量(可以是负的)
/** * Question:只使用处理IO的printDigit函数,编写一个过程以输出任意double型量(可以为负) * @author wulei * 这道题我的理解是使用最基本的System ...
- C语言中printf的规范输出
1.调用格式为 printf("<格式化字符串>", <参量表>); 其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另 ...
- [转载]为什么使用%lf读取double型的值,而用%f进行显示?
博客地址:http://blog.csdn.net/shenzhou111/article/details/7826444 今天看到一篇好文章,mark一下. 出去旅游了一下,所以有些天没敲代码,于是 ...
- 使用random输出10个double型的随机数
使用random输出10个double型的随机数 代码如下: package Day05;import java.util.Random;import java.util.Scanner; publi ...
- ①创建项目testpackage ②在pack2.B中添加方法f ③在类A中添加如下三个成员变量:int型的私有变量i float型的变量f double型的公有变量d 在pack1.B的main方法中为对象a的成员变量f和d分别赋值为2和3 在pack2.C的main方法中为对象a的成员变量d赋值为3
package pack1; public class A { private int i; float f; public double d; public float getF() { retur ...
- double 型变量的输入输出标准格式
c语言double型变量标准输入格式: scanf("%lf",num); 标准输出格式: printf("%f\n",num); 注:有过输出用%lf输出OJ ...
- 最好使用%f输出浮点数据,acm
今天做题的时候发现使用%lf输出的时候总是wrong,而一旦改成%f就ac了,询问学长后知道,不要用%lf输出,浮点都用%f 然而我还是有疑惑,如果%f容不下输出的数据怎么办呢? 于是我就去百度 原来 ...
随机推荐
- GULP入门(一)
1.首先要先装node.然后在命令行里安装全局的gulp: npm install --global gulp 这是gulp在的生成的位置 2.接下来,我们需要将gulp安装到项目本地 npm ins ...
- python生成VOC2007的类库
VOCAnnotation.py: # -*-coding:utf-8-*- from lxml import etree import os class VOCAnnotation(object): ...
- c++设计模式:单例模式
1.设计思想: 单例模式,顾名思义,即一个类只有一个实例对象.C++一般的方法是将构造函数.拷贝构造函数以及赋值操作符函数声明为private级别,从而阻止用户实例化一个类.那么,如何才能获得该类的对 ...
- Django项目:CRM(客户关系管理系统)--34--26PerfectCRM实现King_admin自定义排序
ordering = ['-qq'] #自定义排序,默认'-id' #base_admin.py # ————————24PerfectCRM实现King_admin自定义操作数据———————— f ...
- 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)
本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...
- git bash 常用操作文件命令行
1, cd : change directory的简写,改变目录的意思,就是切换到哪个目录下, 如 cd e:\fff 切换 E 盘下面的fff 目录. 当我们用cd 进入文件夹时,我们可以使用 通配 ...
- Clash Credenz 2014 Wild Card Round题解
A题 简单模拟. /************************************************************************* > File Name: ...
- Tomcat中startup.bat启动无效
error: Linux下启动和关闭tomcat报错,如下图所示: 而在windows下用cmd启动startup.bat也会报如上的错误: Neither the JAVA_HOME nor the ...
- JavaScript 面试:什么是纯函数?
原文:Eric Elliott 译文:众成翻译 www.zcfy.cc/article/master-the-javascript-interview-what-is-a-pure-function ...
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...