%*s 格式说明符 C
printf("%d",printf("%*s%*s",6,"",6))
结果是2个数字相加(6+6),有谁有关于这种类型的格式说明符
printf("%d",printf("%*s%*s",6,"",6))
Results in the addition of the 2 numbers (6+6), does anyone have on such type of format specifiers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例格式错误,“嵌套”
printf
缺少参数。如果你这样写:
if 就可以了。
*
表示必须从printf
的下一个参数读取精度字段。因此,在这种情况下,“嵌套”的printf
打印两个长度最多为 6 的字符串。由于
printf
返回写入的字符数,因此内部printf
code> 返回 12,这是外部 printf 打印的内容。引用手册页的相关部分:
我不确定这有多便携,但我确信有更好的方法来添加两个数字强>。
Your example is malformed, there's a missing argument to the "nested"
printf
.If you write it like this:
if becomes kind of ok. The
*
means that the precision field must be read from the next argument toprintf
. So in this case, the "nested"printf
prints two strings of length at most 6.Since
printf
returns the number of characters written, the innerprintf
returns 12, which the outerprintf
prints.Quote from the relevant part of the man page:
I'm not sure how portable this is, but what I'm sure of is that there are much better ways to add two numbers.
A * 作为宽度说明符表示宽度作为参数传入。
相当于:
这将打印出 12 个空格。
由于
printf
返回打印的字符数,因此它将返回12。原始代码缺少最后一个“”参数。如果它有效,那纯属偶然。
A * as the width specifier indicates that the width is passed in as a parameter.
is equivalent to:
This would print out 12 spaces.
Since
printf
returns the number of characters printed, it will return 12.The original code is missing the final "" parameter. If it works, it is purely by accident.
printf
返回打印的字符数。其余的应该是显而易见的。printf
returns the number of characters printed. The rest should be obvious.