之前没有注意过这个问题,  转自: 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.

为什么printf()用%f输出double型,而scanf却用%lf呢?的更多相关文章

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

    转:https://blog.csdn.net/bat67/article/details/52056057 示例:double x:scanf(“%f”,&x):输入“123.4”,输出x的 ...

  2. double型(双精度型)的打印(printf) 和scanf

    double型,printf()用%f输出,而scanf用%lf来接受输入.   格式 printf scanf %c int char * %d, %i int int * %o, %u, %x u ...

  3. 只使用处理I/O的printDigit方法,编写一种方法一输出任意的double型量(可以是负的)

    /** * Question:只使用处理IO的printDigit函数,编写一个过程以输出任意double型量(可以为负) * @author wulei * 这道题我的理解是使用最基本的System ...

  4. C语言中printf的规范输出

    1.调用格式为  printf("<格式化字符串>", <参量表>);   其中格式化字符串包括两部分内容: 一部分是正常字符, 这些字符将按原样输出; 另 ...

  5. [转载]为什么使用%lf读取double型的值,而用%f进行显示?

    博客地址:http://blog.csdn.net/shenzhou111/article/details/7826444 今天看到一篇好文章,mark一下. 出去旅游了一下,所以有些天没敲代码,于是 ...

  6. 使用random输出10个double型的随机数

    使用random输出10个double型的随机数 代码如下: package Day05;import java.util.Random;import java.util.Scanner; publi ...

  7. ①创建项目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 ...

  8. double 型变量的输入输出标准格式

    c语言double型变量标准输入格式: scanf("%lf",num); 标准输出格式: printf("%f\n",num); 注:有过输出用%lf输出OJ ...

  9. 最好使用%f输出浮点数据,acm

    今天做题的时候发现使用%lf输出的时候总是wrong,而一旦改成%f就ac了,询问学长后知道,不要用%lf输出,浮点都用%f 然而我还是有疑惑,如果%f容不下输出的数据怎么办呢? 于是我就去百度 原来 ...

随机推荐

  1. Maven实战错误笔记:使用mvn archetype:generate报错:Unable to add module to the current project as it is not of packaging type 'pom'

    在使用mvn archetype:generate生成Maven实战03:HelloWorld中的HelloWorld的项目骨架时报了这个错,从字面上分析是可能与pom.xml文件有关,然后我看了一下 ...

  2. IO流16 --- 对象流操作字符串 --- 技术搬运工(尚硅谷)

    序列化 @Test public void test12() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...

  3. Elasticsearch快速开始

    Elasticsearch是一个分布式RESTful风格的搜索和数据分析引擎 查询:Elasticsearch允许执行和合并多种类型的搜索——结构化.非结构化.地理位置.度量指标.搜索方式随心而变 分 ...

  4. PLSQLDeveloper链接报错 解决办法

    PLSQL Developer 9.06.1665中文破解版 亲们,win7 64位系统现在还没有PLSQLDeveloper可以使用,但是怎么办呢.好的,下面教大家怎么在64位系统下安装PLSQLD ...

  5. Python list以及numpy处理技巧

    1.numpy数组后面添加一个list: import numpy as np a=[[1,2,3],[4,5,6],[7,8,9]] box=np.array(a) box=np.vstack((b ...

  6. C++11的for循环的新用法

    字符串 string str = "this is a string"; for(auto ch : str) cout << ch << endl; 等价 ...

  7. Amazon EBS的功能更新

    Amazon EBS(Elastic Block Store.简称EBS) 为 Amazon EC2 实例提供块级存储服务.EBS 卷须要通过网络訪问,而且能独立于实例的生命周期而存在.也就是说假如E ...

  8. 备忘录模式(Memento、Originator、Caretaker)(状态保存,备份恢复)

    定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样就可以将该对象恢复到原先保存的状态 类型:行为类 类图: 我们在编程的时候,经常需要保存对象的中间状态,当需要的时 ...

  9. Docker Tomcat部署

    1.下载tomcat镜像 docker pull tomcat 2.上传项目 /etc/tomcat/webapps/ 3.启动tomcat容器 docker run -d --name tomcat ...

  10. Oracle JOB的建立,定时执行任务

    Oracle JOB的建立,定时执行任务 oracle job的相关设置 next date: 2010-12-28 18:05:00 interval: to_date(to_char(sysdat ...