为什么 scanf() 需要“%lf"?对于双打,什么时候 printf() 可以只使用“%f"? [英] Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

查看:19
本文介绍了为什么 scanf() 需要“%lf"?对于双打,什么时候 printf() 可以只使用“%f"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?

Example code:

double d;
scanf("%lf", &d);
printf("%f", d);

解决方案

Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf, %lg or %le (or %la in C99) to read in doubles.

这篇关于为什么 scanf() 需要“%lf"?对于双打,什么时候 printf() 可以只使用“%f"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆