<分区>
printf("%d",printf("%*s%*s",6,"",6))
结果是 2 个数字 (6+6) 相加,有没有人有这种类型的格式说明符
<分区>
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/