如何使用" ZD"符用`的printf()`? [英] How to use "zd" specifier with `printf()`?

查看:173
本文介绍了如何使用" ZD"符用`的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


这应该被理解为


  1. 以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>和<一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html\"><$c$c><sys/types.h>.虽然POSIX没有明确这么说,presumably ssize_t供将是对应于为size_t 有符号的类型。
    所以,如果你正在编写特定POSIX-code,ZD%是(可能)类型的印刷值的正确格式 ssize_t供

    Looking for clarification on using "zd" with printf().

    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 following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument. C11dr §7.21.6.1 7

    Should this be read as

    1. "z Specifies that a following d ... conversion specifier applies to a size_t or the corresponding signed integer type argument ... "(both types) and "z Specifies that a following u ... conversion specifier applies to a size_t or the corresponding signed integer type argument ..." (both types)

    OR

    1. "z Specifies that a following d ... conversion specifier applies to a corresponding signed integer type argument ..." (signed type only) and "z Specifies that a following u ... conversion specifier applies to a size_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 type size_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 for unsigned long, for example, then "%zd" expects an argument of type long, 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 type size_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 type size_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, presumably ssize_t will be the signed type corresponding to size_t. So if you're writing POSIX-specific code, "%zd" is (probably) the correct format for printing values of type ssize_t.

    这篇关于如何使用&QUOT; ZD&QUOT;符用`的printf()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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