What does "%. * S" mean in printf?

I have a piece of code in which there is

printf("%.*s\n") 

what does %.*s mean?

+69
c printf
Oct 26 2018-11-11T00:
source share
3 answers

You can use an asterisk ( * ) to convey the specifier / precision of the width of printf() , rather than hard-coded it into a format string, i.e.

 void f(const char *str, int str_len) { printf("%.*s\n", str_len, str); } 
+74
Oct 26 '11 at 5:59 a.m.
source share

I do not think the code above is correct, but (according to this description printf() ) .* Means

The width is not indicated in the format string, but as an additional argument of an integer value preceding the argument to be formatted. ''

So this is a string with a valid width as an argument.

+11
Oct 26 '11 at 6:00 a.m.
source share

See: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

.* Accuracy is not indicated in the format string, but as an additional argument of an integer value preceding the argument to be formatted.

s character string

+9
Oct 26 '11 at 6:00 a.m.
source share



All Articles