473,652 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3715
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**********@i nvalid.invalidw rote:
>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.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<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
4513
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
17172
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 = "OneWord"; char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);
5
2197
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
76484
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 me!! What does ### mean in Excel and how do you fix it??? please,please,please help!! 8( I'm totally stressed over this one question!! You can answer here or by email.
2
9046
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. thanks, -Brad
12
8407
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
108086
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 integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context? Thanks
4
3089
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' properties is populated and when page unloading, the resources are cleared. What I want to know is what's happening behind it, that is, from the perspective of the asp.net implementor.....I'm not sure whether i've stated clearly, hope you get it.
20
6843
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 and in end of program it prints that or something else.
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.