c - C中的输入输出

标签 c floating-point printf

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

结果:

input: 10.3

output: 0.00000

为什么?我认为输出应该是 10.3 Visual Studio 2008。

最佳答案

对于scanf()%f 是一个float。对于 double,您需要 %lf。所以,

#include <stdio.h>
main() {
    double d; 
    scanf("%lf", &d); 
    printf("%f\n", d);
}

输入 10.3 产生 10.300000

关于c - C中的输入输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2395853/

相关文章:

c - 如何将 "\r"打印到文件

c - 编写函数输出分数直方图

c - 排除限定词重复的语法

c++ - 从格式为 %.1f 的 printf 中查找舍入数字的意外错误?

javascript - 如何在 JavaScript 中将字符串转换为 float 而不需要 parseFloat 或转换?

javascript - 在 JavaScript 中使用带小数点的 float 的最佳方法是什么?

sprintf 中字符串内的 C 宏

指向 struct 的指针可以别名为 unsigned char 数组吗?

Codeforce 问题 474 A 中给出运行时错误的代码

Python 自动舍入小数