【问题标题】:what "%.*s" means in the asprintf? [duplicate]asprintf 中的“%.*s”是什么意思? [复制]
【发布时间】:2012-12-21 15:31:06
【问题描述】:

可能重复:
what does “%.*s” mean in printf in c

我找到了以下行:

asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)

而我想知道%.*s的含义

【问题讨论】:

  • 你试过文档了吗?
  • 我不知道答案,但用 Google 花了大约一分钟时间。

标签: c


【解决方案1】:

%.*s 格式的意思是“使用 n 个字符的字段宽度打印字符串,其中 n 是从下一个参数中读取的”。

所以在这里,它以size * rxed 个字符的宽度打印buffer。 (必要时用空格填充)

【讨论】:

    【解决方案2】:

    我强烈建议您阅读手册...

    .* 在格式字符串中的意思是:

    精度未在格式字符串中指定,而是作为必须格式化的参数之前的附加整数值参数。

    详情可见here

    所以你没有提供任何细节,但如果:size * rxed 的结果是 5,那么你可以这样做:

    asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
    

    asprintf(&c, "%s%5s", *msg_in, buffer)
    

    同样的效果。

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2023-02-20
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 2020-06-06
      相关资源
      最近更新 更多