如何使用" ZD"符用`的printf()`? [英] How to use "zd" specifier with `printf()`?
问题描述
寻找澄清关于使用ZD
与的printf()
。
当然,以下是C99正确及更高版本。
无效print_size(为size_t SZ){
的printf(%祖\\ n,SZ);
}
的C规格的看起来的允许的printf(%ZD \\ n,SZ)
取决于它是如何阅读:
7.21.6.1的 fprintf中
函数
以Z
指定后续的D
,I
,0
,U
,X
或X
转换说明适用于为size_t
或相应的符号整型参数;或者说一个如下的N
转换说明适用于指针对应的为size_t
参数的符号整型。 C11dr§7.21.6.17
块引用>这应该被理解为
以Z
指定后续的D
...转换说明适用于为size_t
或相应的符号整型参数...(两种)和以Z
指定后续的ū
...转换说明适用于为size_t
或相应的符号整型参数...(两种)或
<醇开始=2>以Z
指定后续的D
...转换说明适用于相应的符号整型参数。 ..(符号类型只)和以Z
指定后续的U
...转换说明适用于为size_t
(无符号类型只)。我已经使用了#2的定义,但现在不那么肯定。
这是正确的,1,2,或其他什么东西?
如果#2是正确的,什么是一个类型,可以使用的例子
ZD%
?
块引用>解决方案
的printf
与%ZD
格式要求对应于无符号类型为size_t
的签署类型的参数。标准C不为这种类型或一个很好的方式,以确定它是什么提供一个名称。如果
为size_t
是无符号长
,例如,那么%ZD的typedef
预计类型的参数长
,但这不是便携式的假设。该标准要求相应的符号和无符号类型用于该重新presentable这两种类型的非负值同样进行再presentation。一个脚注说,这是为了暗示他们是作为函数参数互换。因此,这:
为size_t S = 42;
的printf(S =%ZD \\ N,S);应该工作,并应打印出42。它将间preT值
42
,无符号的类型的为size_t
,就好像它是对应的符号的类型。但是,真的没有很好的理由这样做,因为%俎
也是正确的,明确的,而不是诉诸额外的语言规则。和%俎
工程的所有的类型的值为size_t
,包括外范围对应的符号类型。最后,POSIX定义了一个标题类型
ssize_t供
<$c$c><unistd.h>$c$c>和<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html\"><$c$c><sys/types.h>$c$c>.虽然POSIX没有明确这么说,presumablyssize_t供
将是对应于为size_t
有符号的类型。
所以,如果你正在编写特定POSIX-code,ZD%
是(可能)类型的印刷值的正确格式ssize_t供
。Looking for clarification on using
"zd"
withprintf()
.Certainly the following is correct with C99 and later.
void print_size(size_t sz) { printf("%zu\n", sz); }
The C spec seems allow
printf("%zd\n", sz)
depending on on how it is read:7.21.6.1 The
fprintf
function
z
Specifies that a followingd
,i
,o
,u
,x
, orX
conversion specifier applies to asize_t
or the corresponding signed integer type argument; or that a followingn
conversion specifier applies to a pointer to a signed integer type corresponding tosize_t
argument. C11dr §7.21.6.1 7Should this be read as
- "
z
Specifies that a followingd
... conversion specifier applies to asize_t
or the corresponding signed integer type argument ... "(both types) and "z
Specifies that a followingu
... conversion specifier applies to asize_t
or the corresponding signed integer type argument ..." (both types)OR
- "
z
Specifies that a followingd
... conversion specifier applies to a corresponding signed integer type argument ..." (signed type only) and "z
Specifies that a followingu
... conversion specifier applies to asize_t
" (unsigned type only).I've been using the #2 definition, but now not so sure.
Which is correct, 1, 2, or something else?
If #2 is correct, what is an example of a type that can use
"%zd"
?
解决方案
printf
with a"%zd"
format expects an argument of the signed type that corresponds to the unsigned typesize_t
.Standard C doesn't provide a name for this type or a good way to determine what it is. If
size_t
is a typedef forunsigned long
, for example, then"%zd"
expects an argument of typelong
, but that's not a portable assumption.The standard requires that corresponding signed and unsigned types use the same representation for the non-negative values that are representable in both types. A footnote says that this is meant to imply that they're interchangeable as function arguments. So this:
size_t s = 42; printf("s = %zd\n", s);
should work, and should print "42". It will interpret the value
42
, of the unsigned typesize_t
, as if it were of the corresponding signed type. But there's really no good reason to do that, since"%zu"
is also correct and well defined, without resorting to additional language rules. And"%zu"
works for all values of typesize_t
, including those outside the range of the corresponding signed type.Finally, POSIX defines a type
ssize_t
in the headers<unistd.h>
and<sys/types.h>
. Though POSIX doesn't explicitly say so, presumablyssize_t
will be the signed type corresponding tosize_t
. So if you're writing POSIX-specific code,"%zd"
is (probably) the correct format for printing values of typessize_t
.这篇关于如何使用&QUOT; ZD&QUOT;符用`的printf()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!