 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tingjun.li@gmail.com Guest
|
Posted: Fri Jun 09, 2006 9:10 am Post subject: What does "%.*s" stand for? |
|
|
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 seems printf("%d %s", num,
string) is mostly used.. |
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Jun 09, 2006 9:10 am Post subject: Re: What does "%.*s" stand for? |
|
|
tingjun.li (AT) gmail (DOT) com wrote:
| Quote: | 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.
| Quote: | 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. |
|
| Back to top |
|
 |
lyang2@cisco.com Guest
|
Posted: Fri Jun 09, 2006 9:10 am Post subject: Re: What does "%.*s" stand for? |
|
|
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
tingjun.li (AT) gmail (DOT) com wrote:
| Quote: | 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 seems printf("%d %s", num,
string) is mostly used.. |
|
|
| Back to top |
|
 |
tingjun.li@gmail.com Guest
|
Posted: Mon Jun 12, 2006 9:10 am Post subject: Re: What does "%.*s" stand for? |
|
|
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
---
Christopher Benson-Manica 写道:
| Quote: | lyang2 (AT) cisco (DOT) com <yangliu.seu (AT) gmail (DOT) com> wrote:
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.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|