格式说明符“%qd"的目的是什么?在`printf()`中? [英] What is the purpose of format specifier "%qd" in `printf()`?

查看:219
本文介绍了格式说明符“%qd"的目的是什么?在`printf()`中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览时,我看到了格式说明符%qd github 代码.然后我检查了GCC编译器,它工作正常.

I saw format specifier %qd when browsing github code. Then I checked in GCC compiler, it's working fine.

#include <stdio.h>

int main()
{  
    long long num = 1;
    printf("%qd\n", num);
    return 0;
}

printf()中的格式说明符%qd的用途是什么?

What is the purpose of format specifier %qd in printf()?

推荐答案

尽管在正常的google搜索中只有几篇关于%qd的文章,以供将来参考,但此答案是我自己的研究的汇编, rsp 的答案和此处 StoryTeller .

Though only a few articles come about %qd in a normal google search, for future reference, this answer is the compilation of my own research, rsp's answer and little discussions here in the comments section by Jonathan Leffler and StoryTeller.

%qd旨在舒适地处理所有64位 机器,最初是bsd-ism(quad_t).

%qd was intended to handle 64 bits comfortably on all machines, and was originally a bsd-ism (quad_t).

但是,egcs(和之前的gcc)将其等同于ll, 并不总是等效的:配置了openbsd-alpha使得long为 64位,因此将quad_t类型定义为long. 在这种特殊情况下,类似于printf的属性无法正常工作

However, egcs (and gcc before that) treats it as equivalent to ll, which is not always equivalent: openbsd-alpha is configured so that long is 64 bits, and hence quad_t is typedef'ed to long. In that particular case, the printf-like attribute doesn't work as intended.

如果在openbsd-alpha上为sizeof(long long) == sizeof(long),它应该可以工作 无论如何-%ld%lld%qd应该可以互换.在OpenBSD/alpha上,sizeof(long) == sizeof(long long) == 8.

If sizeof(long long) == sizeof(long) on openbsd-alpha, it should work anyway - i.e. %ld, %lld, and %qd should be interchangeable. On OpenBSD/alpha, sizeof(long) == sizeof(long long) == 8.

在广泛使用ISO C99扩展之前,出现了几种特定于平台的长度选项,其中q是其中之一.它用于整数类型,这会导致printf期望使用64位(四字)整数参数.它通常在BSD平台中找到.

Several platform-specific length options came to exist prior to widespread use of the ISO C99 extensions, q was one of them. It was used for integer types, which causes printf to expect a 64-bit (quad word) integer argument. It is commonly found in BSD platforms.

但是,C99和C11均未提及长度修饰符q. fprintf()的macOS(BSD)手册页将q标记为已弃用.因此,建议使用ll代替q.

However, both of the C99 and C11 says nothing about length modifier q. The macOS (BSD) manual page for fprintf() marks q as deprecated. So, using ll is recommended in stead of q.

参考:

https://gcc.gnu.org/ml/gcc-bugs/1999-02n/msg00166.html

https://en.wikipedia.org/wiki/Printf_format_string

https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p7

这篇关于格式说明符“%qd"的目的是什么?在`printf()`中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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