By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

what does "*" inside a printf mean?

P: n/a
hello everybody,
as i was going through the "printf" man page, i came across this
statement.

printf("%*d", width, num);
what does "*" mean?

Sep 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
chandanlinster wrote:
as i was going through the "printf" man page, i came across this
statement.

printf("%*d", width, num);
what does "*" mean?
Reread the man page, it is explained there :-)

You have several things that can be found between the '%' and
the "conversion specifier", in this case 'd':
- flags (modify left-/right-justified and several other aspects)
- fieldwidth (how many characters are at least output)
- precision (various meanings)
- length modifier (e.g. 'l' which means in our example "long int
argument")

What you have here is the fieldwidth. This can be a number or
a '*'.
printf("%2d", num);
is equivalent to
printf("%*d", 2, num);
Advantage of the '*': You can determine the format at runtime.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #2

P: n/a
In article <4o************@individual.net>,
Michael Mair <Mi**********@invalid.invalidwrote:
>You have several things that can be found between the '%' and
the "conversion specifier", in this case 'd':
[...]
>- precision (various meanings)
The precision modifier is particular useful for printing
non-null-terminated strings:

printf("%.*s\n", len, str);

-- Richard
Sep 29 '06 #3

P: n/a
chandanlinster wrote:
>
as i was going through the "printf" man page, i came across this
statement.

printf("%*d", width, num);
what does "*" mean?
It's in the standard. From N869:

[#5] As noted above, a field width, or precision, or both,
may be indicated by an asterisk. In this case, an int
argument supplies the field width or precision. The
arguments specifying field width, or precision, or both,
shall appear (in that order) before the argument (if any) to
be converted. A negative field width argument is taken as a
- flag followed by a positive field width. A negative
precision argument is taken as if the precision were
omitted.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Oct 1 '06 #4

P: n/a
Richard Tobin wrote:
The precision modifier is particular useful for printing
non-null-terminated strings:

printf("%.*s\n", len, str);
Absolutely. This is an idiom I use constantly when working with stdio and TLV
or general network data/membufs and believe it to be an underrated feature.
Before, I used to just use a buffer and memcpy + nul-term it when I needed to
output something, but after I discovered "%.*s", no need anymore.
Oct 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.