c - %*s 格式说明符 C

标签 c printf scanf

<分区>

printf("%d",printf("%*s%*s",6,"",6))

结果是 2 个数字 (6+6) 相加,有没有人有这种类型的格式说明符

最佳答案

您的示例格式错误,“嵌套”printf 缺少参数。

如果这样写:

printf("%d",printf("%*s%*s",6,"",6, ""));

如果变得有点好。 * 表示必须从 printf 的下一个参数中读取精度字段。所以在这种情况下,“嵌套”printf 打印两个长度最多为 6 的字符串。

由于 printf 返回写入的字符数,内部 printf 返回 12,外部 printf 打印。

引自手册页的相关部分:

The precision

An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int. If the precision is given as just '.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix character for a, A, e, E, f, and F con‐ versions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s and S conversions.

我不确定这有多便携,但我敢肯定的是,有更好的方法来将两个数字相加

关于c - %*s 格式说明符 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111628/

相关文章:

C - 递归复制文件

c - 如何转发/多路复用 ioctl 组?

bash - 填充的 printf 格式字符串没有使用多字节字符添加足够的填充

c - scanf ("%s")、sscanf ("%s") 和 scanf ("%s ") 之间有什么区别?

无法理解这个无限循环

c - 如何使用C语言程序通过计算机的usb端口打开/关闭电灯泡

C++ - 数组的初始值设定项太多

c - 通过 snprintf 在 C 中填充静态字符串缓冲区

c - 尽管禁用了缓冲,但 Printf 没有立即打印

执行 scanf() 并获取用户输入后,C 程序终止且没有任何错误