What does "%.*s" stand for?

T

tingjun.li

There is a demo program:
-------------------------------------------------

const char* WS = " \t\n";
const int n_WS = strlen(WS);

char* s1 = "This sentence contains five words.";
char* s2 = "OneWord";

char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);

char* end2 = find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);

printf("First word of s1: %.*s", end1 - s1, s1);
 
R

Rolf Magnus

There is a demo program:
-------------------------------------------------

const char* WS = " \t\n";
const int n_WS = strlen(WS);

char* s1 = "This sentence contains five words.";
char* s2 = "OneWord";

char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);

char* end2 = find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);

printf("First word of s1: %.*s", end1 - s1, s1);

It means that the first variable argument is printed as a string and the
size for it is taken from the second one.
It seems printf("%d %s", num, string) is mostly used..

That wouldn't work as desired, because it would print everything up to the
terminating '\0' character.
 
L

lyang2

for s type, the integer value after dot (.) in the format string mean
the maximum number of characters to be printed. So I think * mean all
the characters will be printed out until first null character is
encountered.

Regards.

Kandy
 
R

Richard Herring

There is a demo program:
-------------------------------------------------

const char* WS = " \t\n";
const int n_WS = strlen(WS);

char* s1 = "This sentence contains five words.";
char* s2 = "OneWord";

char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);

char* end2 = find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);

printf("First word of s1: %.*s", end1 - s1, s1);
The documentation for printf that came with your compiler should explain
this. The full form of a format-specifier is

% [flags] [width] [.precision] [modifier] type

Here * is the precision field, s is the type and the other fields are
omitted. For string output, "precision" denotes the maximum number of
characters to output.

An asterisk within a format specifier where a number might occur means
that the next argument to printf is to be used as that number.

So it means "print at most (end1-s1) characters of the null-terminated
string starting at s1".
 
I

Ivan Vecerina

: (e-mail address removed) wrote:
:
: > There is a demo program:
: > -------------------------------------------------
: >
: > const char* WS = " \t\n";
: > const int n_WS = strlen(WS);
: >
: > char* s1 = "This sentence contains five words.";
: > char* s2 = "OneWord";
: >
: > char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);
: >
: > char* end2 = find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);
: >
: > printf("First word of s1: %.*s", end1 - s1, s1);
: >
: > -------------------------------------------------
: >
: > I don't know What does "%.*s" stand for?
:
: It means that the first variable argument is printed as a string and the
: size for it is taken from the second one.

Actually, the *first* parameter is the length to be printed.

To the OP: the * can be used instead of a width or precision specification
(also for %d, %f, etc) to indicate that the width value should be read
as the next function parameter (yet located before the value being
converted).

See for example: http://www.mkssoftware.com/docs/man1/printf.1.asp


hth -Ivan
 
C

Christopher Benson-Manica

for s type, the integer value after dot (.) in the format string mean
the maximum number of characters to be printed. So I think * mean all
the characters will be printed out until first null character is
encountered.

Wrong. See other replies for the right answer.
 
T

tingjun.li

Thank you very much for all of your replies.

Now I know the answer: Thanks!

---

In a string, precision is the maximum number of bytes to be printed
from the string; in a 'br number, the precision is the number of digits
to be printed to the right of the decimal point in a floating point
value. width or precision may be specified as *, in which case the
value is read from the next argument, which must be an integer. For
example:

printf "%*.*d\n" 20 10 200

is equivalent to

printf "%20.10d\n" 200
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top