473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

what does "*" inside a printf mean?

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
4 3691
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marek Kurowski | last post by:
Yo! What mean when program is ALPHA or BETA version? I suppose it is not release version of program, but I don't know what it exactly mean. What it mean in your opinion? Marek Kurowski
6
by: tingjun.li | last post by:
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 =...
5
by: chandanlinster | last post by:
consider the program, /*********************************************************/ int main() { int a; printf("a = %u\n", a); printf("*a = %u\n", *a); return 0; }
9
by: plusk1008 | last post by:
I have finals next week and I am stuck on one question on my review sheet for excel. So once again I beg: Please, please, please, please, please, please, please, please, please, please someone help...
2
by: ledzepfan32 | last post by:
i have a question: what does printf do? i have seen it a lot, but cannot find a clear description of what it does. in my learn c++ in 21 days book, it states that it will not teach it in the book....
12
by: hn.ft.pris | last post by:
I know int *p mean an array of pointers to int, but I meet int (*p) which is confusing. What does it mean and how to use it? Thanks for help.
16
by: macmartin | last post by:
Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like: flag = (i%primes); In this case, the variable 'i', 'j' and 'flag' are...
4
by: lander | last post by:
I've read the page life cycle thing in msdn, still, i'm getting a bit confused of thinking how all the things are going under the hood... I know that when page loading, that the controls'...
20
by: Sanchit | last post by:
I want to know how does printf (stdio library function) works? Does this depand on complier (I am using gcc on Linix) Does it uses some buffer in which it stores all what needed to be printed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.