开发者

How do I use multiple precisions in printf()?

Looking at the information under t开发者_StackOverflow社区he heading "Precision can be omitted or be any of:".

The example: printf("%.*s", 3, "abcdef"); works, outputting:abc (truncating the rest of the string.)

Now, I would like to have a string with multiple parameters formatted (truncated):

printf("%.*s, %.*s", 3, 3, "abcdef", "xyz123");

but the program crashes.

What is the correct syntax?

Thank You.


Maybe you should change order?

printf("%.*s, %.*s", 3, "abcdef", 3, "xyz123");

By the way you can hardcode precision if you don't need it as a variable:

printf("%.3s, %.3s", "abcdef", "xyz123");

(Stephen Canon kindly corrected the typo)


You want to do it like this:

printf("%.*s, %.*s", 3, "abcdef", 3, "xyz123");

The format arguments should be in the same order as the format specifiers.


printf("%.*s, %.*s",3,"abcdef",3,"xyz123");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜