开发者

How do I use multiple precisions in printf()?

开发者 https://www.devze.com 2022-12-28 10:26 出处:网络
Looking at the information under t开发者_StackOverflow社区he heading \"Precision can be omitted or be any of:\".

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

精彩评论

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