使用%D和%F c的printf的 [英] C printf using %d and %f

查看:145
本文介绍了使用%D和%F c的printf的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的这个程序,我注意到,使用%F为一个浮动一张双人床和%d个给了我完全不同的东西。任何人都知道为什么发生这种情况?

  INT的main()
{
 浮动= 1F;
 双B = 1; 的printf(浮=%d个\\ ndouble =%F,A,B);
}

这是输出

 浮= -1610612736
双= 190359837192766135921612671364749893774625551025007120912096639276776057269784974988808792093423962875123204096.0000


解决方案
由于的方式可变参数的工作原理

,C有没有你实际上是通过它除了%d个<值类型的想法/ code>和%F 。当你在你基本上做(无效*)及可变的参数传递; myvalue的因为无论是编译器,也没有在运行时函数可以判断变量的除了该类型什么是格式化字符串给出。因此,即使有一个隐式转换可用,编译器不知道它是必要的。

好吧,双是大多数系统的8个字节,而持股量为4字节。因此,这两种类型都没有二进制兼容的,因为它是。它是象设法间preT一个字符串作为双,或其他一些不兼容的类型。

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?

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

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

This is the output

float = -1610612736
double = 190359837192766135921612671364749893774625551025007120912096639276776057269784974988808792093423962875123204096.0000

解决方案

Because of the way variable parameters work, C has no idea of the type of value you are actually passing it other than %d and %f. When you pass in a variable parameter you are basically doing (void*)&myvalue because 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.

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.

这篇关于使用%D和%F c的printf的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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