gpt4 book ai didi

c - 将 uintmax_t 或 size_t 传递给自定义 printf 转换说明符

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:11 24 4
gpt4 key购买 nike

我正在编写一个转换说明符 ( %b) 以便能够以二进制形式打印任何无符号整数类型。

这是它的完整代码:https://codereview.stackexchange.com/questions/219994/register-b-conversion-specifier

现在似乎一切正常,但有可能

sizeof(uintmax_t) > sizeof(unsigned long long)

size_t相同

而且 glibc 似乎只传递有关“正常”整数类型的信息(unsigned + 长度修饰符或 char)。现在就不可能为此做好准备吗?可能是因为还没有实现这两种类型更宽的(AFAIK)。

glibc 如何处理 jz长度修饰符?可能它们只是被注册为等同于 ll ,并且没有不同的处理方式。

编辑:

似乎有一些暗示可能存在处理基本类型以外的类型的可能性。

来自 <printf.h> :

/* Type of a printf specifier-arginfo function.
INFO gives information about the format specification.
N, ARGTYPES, *SIZE has to contain the size of the parameter for
user-defined types, and return value are as for parse_printf_format
except that -1 should be returned if the handler cannot handle
this case. This allows to partially overwrite the functionality
of existing format specifiers. */

typedef int printf_arginfo_size_function (const struct printf_info *__info,
size_t __n, int *__argtypes,
int *__size);

参见 *SIZE又名 int *__size

还有另一个关于已弃用的旧函数的提示:

/* Old version of 'printf_arginfo_function' without a SIZE parameter. */

typedef int printf_arginfo_function (const struct printf_info *__info,
size_t __n, int *__argtypes);

/* Obsolete interface similar to register_printf_specifier. It can only
handle basic data types because the ARGINFO callback does not return
information on the size of the user-defined type. */

extern int register_printf_function (int __spec, printf_function __func,
printf_arginfo_function __arginfo)
__THROW __attribute_deprecated__;

最佳答案

Is it just impossible right now to prepare for that?

是的。

struct printf_info 未准备好处理具有许多真实整数大小的实现。

相反,当今天使用 j, z, t 修饰符时,它以某种方式映射为 nonel、ll

int128_t, int256_t, int512_t, int1024_t出来,肯定struct printf_info会进化。只是不知道今天是什么。

关于c - 将 uintmax_t 或 size_t 传递给自定义 printf 转换说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56068734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com