gpt4 book ai didi

c++ - 求C++11 printf format args that consume more than one values的解释

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:49 24 4
gpt4 key购买 nike

我已经开始在大量使用 C++11 的代码库中工作。我正在寻找许多资源来帮助我学习,但我找不到任何关于我看到使用的 printf 格式字符串类型的提及:

printf( "%.*s", integerValue, charPointer );

我想我知道它在做什么:integerValue 是字符缓冲区的长度。然而,我数十年的 C/C++ 经验告诉我,格式字符串中只有一个参数规范,但它在格式字符串之后使用了两个值。

这种新奇的行为记录在哪里?是否有更多像这样的多值消耗 printf 格式参数?我一直在搜索 printf 格式字符串文档,但一无所获。

最佳答案

Here是格式说明符的一个很好的引用。那么,在某些细节上,转换规范包括:

  • introductory % character
  • (optional) one or more flags that modify the behavior of the conversion:
    • -: the result of the conversion is left-justified within the field (by default it is right-justified)
    • +: the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative)
    • space: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if + flag is present.
    • # : alternative form of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined.
    • 0 : for integer and floating point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if - flag is present.
  • (optional) integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)
  • (optional) . followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero. See the table below for exact effects of precision.
  • (optional) length modifier that specifies the size of the argument
  • conversion format specifier

(强调我的)。

因此,在使用 * 作为字段宽度或(在 . 之后)作为精度时,您需要指定一个额外的整数参数。

关于c++ - 求C++11 printf format args that consume more than one values的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637275/

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