C语言 C printf 使用 %d 和 %f

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:30:59  来源:igfitidea点击:

C printf using %d and %f

cprintingfloating-pointdoubleprintf

提问by user69514

I was working on this program and I noticed that using %f for a double and %d for a float gives me something completely different. Anybody knows why this happens?

我正在研究这个程序,我注意到使用 %f 表示 double 和 %d 表示浮点数给了我完全不同的东西。有人知道为什么会这样吗?

int main ()
{
 float a = 1F;
 double b = 1;

 printf("float =%d\ndouble= %f", a, b);
}

This is the output

这是输出

float = -1610612736
double = 190359837192766135921612671364749893774625551025007120912096639276776057269784974988808792093423962875123204096.0000

采纳答案by Earlz

Because of the way variable parameters work, C has no idea of the type of value you are actually passing it other than %dand %f. When you pass in a variable parameter you are basically doing (void*)&myvaluebecause neither the compiler, nor the function at run time can determine the type of the variable except for what is given in the formatting string. So even though there is an implicit conversion available, the compiler does not know it is needed.

由于可变参数的工作方式,除了%dand之外,C 不知道您实际传递的值的类型%f。当您传入一个变量参数时,您基本上是在做,(void*)&myvalue因为编译器和运行时的函数都不能确定变量的类型,除了格式化字符串中给出的类型。因此,即使有可用的隐式转换,编译器也不知道需要它。

Well, double is 8 bytes on most systems while float is 4 bytes. So the two types are not binary compatible as it is. And it is like trying to interpret a string as a double, or some other incompatible type.

好吧,在大多数系统上,double 是 8 个字节,而 float 是 4 个字节。所以这两种类型不是二进制兼容的。这就像试图将字符串解释为双精度型或其他一些不兼容的类型。

回答by Tronic

%dstands for decimal and it expects an argument of type int(or some smaller signed integer type that then gets promoted). Floating-point types floatand doubleboth get passed the same way (promoted to double) and both of them use %f. In C99 you can also use %lfto signify the larger size of double, but this is purely cosmetic (notice that with scanfno promotion occurs and this actually makes a difference).

%d代表十进制,它需要一个类型的参数int(或一些较小的有符号整数类型,然后被提升)。浮点类型floatdouble两者都以相同的方式传递(提升为double)并且它们都使用%f. 在 C99 中,您还可以使用%lf来表示 的较大尺寸double,但这纯粹是装饰性的(注意,scanf没有进行升级,这实际上有所不同)。

回答by Pablo Santa Cruz

It has to do with internal representations of data. You are trying to print a float as if it is an int, which has a completely different internal (binary) representation.

它与数据的内部表示有关。您正在尝试打印一个浮点数,就好像它是一个 int 一样,它具有完全不同的内部(二进制)表示。

The doublecase is similar. You probably have garbage (any data) after the float variable. That garbage is interpreted as part of the double value.

双重的情况是相似的。您可能在 float 变量之后有垃圾(任何数据)。该垃圾被解释为 double 值的一部分。

回答by GreenMatt

%d prints an integer, so your printf is interpreting your variable a as an int.

%d 打印一个整数,因此您的 printf 将您的变量 a 解释为一个 int。

With gcc 3.4.6, I get 0's for both values with your code (after taking the F off the a initialization, as that led to a compiler error). I suspect this has to do with the part of the a variable that is being interpreted as an int being 0, and then the compiler gets confused ... (there's probably a better explanation, but I can't think of it).

使用 gcc 3.4.6,我的代码中的两个值都为 0(在将 F 关闭初始化之后,因为这导致编译器错误)。我怀疑这与 a 变量被解释为 int 为 0 的部分有关,然后编译器会感到困惑......(可能有更好的解释,但我想不出)。

Using %f for both variable prints 1.000000 for both variables for me.

对我来说,对两个变量使用 %f 会为两个变量打印 1.000000。

You can find a list of the conversion characters near the bottom this page:

您可以在本页底部附近找到转换字符列表:

http://www.exforsys.com/tutorials/c-language/managing-input-and-output-operations-in-c.html

http://www.exforsys.com/tutorials/c-language/managing-input-and-output-operations-in-c.html