what does "*" inside a printf mean?

C

chandanlinster

hello everybody,
as i was going through the "printf" man page, i came across this
statement.

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

Michael Mair

chandanlinster said:
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
 
R

Richard Tobin

Michael Mair said:
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
 
C

CBFalconer

chandanlinster said:
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:
< <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/>
 
C

Christopher Layne

Richard said:
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.
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top