使用printf函数 [英] Using printf function

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

问题描述

当我开始学习没有C的C ++时,我从不打扰寻找printf。现在我想在某些项目中使用格式化输出。所以我正在寻找一些可以解释使用printf和IO流之间区别的引用。

I've never bothered to look for printf as I started learning C++ without C. Now i want to use formatted output in some project. so I'm looking for some references that can explain the difference between using printf and IO streams.

我怀疑的是:

float f = 1.5;
printf("%d", f);

为什么打印为零? FWIK它应该重新解释float为int是真的吗?

Why does it print zero? FWIK it should be reinterpreting float as int is that true?

推荐答案

它实际上调用了未定义的行为(UB)。如果格式说明符与传递给 printf 的参数的类型不匹配,那么它就是UB,这意味着任何事情都可能发生。它可以打印 0 ,或 9878978 或任何垃圾。任何完全不可预测的事情都可能发生。

It actually invokes undefined behavior (UB). If the format specifier doesn't match with the type of the argument passed to printf, then it is UB, which means anything could happen. It can print 0, or 9878978 or any garbage. Anything, entirely unpredictable, could happen.

如果参数的数据类型是 float %f $ c>。

Use %f if the data type of the argument is float.

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

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