- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
除了 %hn
和 %hhn
(其中 h
或 hh
指定 < em>指向对象),printf
格式说明符的h
和hh
修饰符有什么意义?
由于标准要求默认提升应用于可变参数函数,因此不可能传递类型为 char
或 short
(或任何有符号/其未签名的变体)到 printf
。
根据 7.19.6.1(7),h
修饰符:
Specifies that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to short int or unsigned short int before printing); or that a following n conversion specifier applies to a pointer to a short int argument.
如果参数实际上是 short
或 unsigned short
类型,则提升为 int
,然后转换回 short
或 unsigned short
将产生与提升为 int
相同的值,而无需任何返回转换。因此,对于 short
或 unsigned short
类型的参数,%d
、%u
等应该给出相同的结果为 %hd
、%hu
等(同样适用于 char
类型和 hh
)。
据我所知,h
或 hh
修饰符可能有用的唯一情况是参数将其传递给 int
超出 short
或 unsigned short
的范围,例如
printf("%hu", 0x10000);
但我的理解是,像这样传递错误的类型无论如何都会导致未定义的行为,因此您不能期望它打印 0。
我见过的一个真实案例是这样的代码:
char c = 0xf0;
printf("%hhx", c);
作者希望它打印 f0
,尽管实现有一个带符号的普通 char
类型(在这种情况下,printf("%x", c)
将打印 fffffff0
或类似内容)。但这种期望是否合理?
(注意:原来的类型是 char
,它被提升为 int
并转换回 unsigned char
char
,从而改变打印的值。但是标准是否指定了这种行为,或者它是损坏的软件可能依赖的实现细节?)
最佳答案
一个可能的原因:为了在格式化输入函数中使用那些修饰符的对称性?我知道这不是绝对必要的,但也许可以看到它的值(value)?
虽然他们没有在 the C99 Rationale document 中提及对称性对于修饰符“h”和“hh”的重要性,委员会确实提到它作为为什么 fscanf()
支持“%p”转换说明符的考虑因素(尽管这对于 C99 来说不是新的 - C90 中支持“%p” ):
Input pointer conversion with %p was added to C89, although it is obviously risky, for symmetry with fprintf.
在关于 fprintf()
的部分中,C99 基本原理文档确实讨论了添加了“hh”,但只是让读者引用 fscanf()
部分:
The %hh and %ll length modifiers were added in C99 (see §7.19.6.2).
我知道这是一个脆弱的线索,但我还是在猜测,所以我想我会给出可能存在的任何论据。
此外,为了完整起见,“h”修饰符出现在原始的 C89 标准中 - 即使由于广泛使用而并非绝对必要,即使可能没有技术要求,它也会存在使用修饰符。
关于c - printf 的 h 和 hh 修饰符的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41521817/
我有两个 javascript 变量(它们来自文本框,所以它们可能被评估为字符串),格式如下:02:30 和 4:45 它们代表小时和分钟。 我想将它们加在一起得到 07:15,但我无法计算出来。 最
我有时间数据库,我想获得时间序列数据的每小时平均值。 示例数据: 1 -1.64 2007-09-29 00:01:09 2 -1.76 2007-09-29 00:03:09 3
我有一个项目,其中使用 Bison 来生成解析器。项目本身是用 SCons 构建的,我的所有代码都是用 C++ 编写的。我一开始就决定将代码分成 3 个主要目录:includes、src 和 test
我正在尝试编写一个将返回当前时间的 SQL 查询,如下所示: HH:MM - HH:MM (+1) 例如如果时间是 14:00,它将返回 14:00 - 15:00 我已经设法做到了以下几点: SEL
关闭。这个问题需要更多focused 。它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 已关闭 3 年前。 Improve this qu
这是我的原始代码- String dateString = "23 Dec 2015 1:4 PM"; Locale locale = new Locale("en_US"); SimpleDateF
我们正在访问允许我们安排日期/时间的 API。我们需要检查预定的事情,这样我们就不会在同一时间重复预订。 API 返回的时间与军事中的 HH 一样。但是,它使用的是 UTC HH。因此,如果我们在下午
final String sourceDate = "05.12.2014 12:17"; final String testDate = "05.12.2014 13:17"; fi
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
kk:mm、HH:mm 和 hh:mm 格式有什么区别?? SimpleDateFormat broken = new SimpleDateFormat("kk:mm:ss"); br
在单元格 B239 中,我有 37:15,代表某人完成一项任务所花费的总小时数。在单元格 C239 中,我有以下公式 =INT(B239)*24+小时(B239)+圆形(分钟(B239)/60,2)
我需要在从 HH: 05 到 HH: 59 的时间每小时列出 MAX () 温度 我有 mySQL 代码: SELECT FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(
我正在使用 PHP 和 MySQL。如何将格式为 HH:MM AM/PM 的时间转换为 HH:MM:SS 以节省 MySQL 的时间? 最佳答案 使用 strtotime 将其转换为时间戳,然后使用
我想知道是否可以使用 moment.js 将 sql TIME (HH:mm:ss) 转换为 HH:mm。或者也许有一个简单的 JavaScript/jQuery 解决方案? 最佳答案 是的,你可以:
我正在寻找一个正则表达式来匹配不同的时间格式(用于时间跟踪网络应用程序)在 javascript 中。用户可以使用以下格式之一插入时间: h hh:mm hh.mm 我可以很容易地创建一个正则表达式来
我们如何在java中比较日期并获取剩余的可用插槽? Sun 10:00-11:00 Sun 12:00-14:00 Sun 15:00-16:00 那么除了这三个时段之外,我们如何才能获得一天中的所有
如何在 SQL Server 中将 hh:mm:ss 转换为 hh:mm? select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Da
假设我的时间是 13:45。 现在我想使用 moment.js 将其转换为 13:45:00。 我在做moment('13:45').format('h:m:s'); 但它不起作用。 最佳答案 使用m
我想将时间从 hh:mm:ss 转换为 hh:mm它以 hh:mm:ss 的形式来自数据库(我的 sql)。我尝试了以下代码,但没有得到我想要的。 try { s= Hibernat
我希望这对某人来说是小菜一碟!目前,所有内容的字符串输出均为上午 12:00。 以下来自 MySQL 的代码,格式为 HH:MM:SS (hours_open, hours_close) $get_h
我是一名优秀的程序员,十分优秀!