473,396 Members | 1,714 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,396 software developers and data experts.

Writing arguments under format specifiers

Does anyone ever write the arguments to printf()-like functions under
the corresponding format specifier?

printf("My name is %s and I am %u years old\n",
name, age);

If the arguments are too long, you could put them on the next line

printf("You got %u/%u (%.2f %%) right\n",
numRight,
numTotal,
(100.*numRight)/numTotal);
The intent is to make it easier to match an argument with the
corresponding format specifier. What do you think?

-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #1
4 1930
Peter Ammon wrote:
Does anyone ever write the arguments to printf()-like functions under
the corresponding format specifier?

printf("My name is %s and I am %u years old\n",
name, age);

If the arguments are too long, you could put them on the next line

printf("You got %u/%u (%.2f %%) right\n",
numRight,
numTotal,
(100.*numRight)/numTotal);
The intent is to make it easier to match an argument with the
corresponding format specifier. What do you think?


Although (in the spirit of literate programming) I pay a lot
of attention to coding style, I've never ever had the urge to
do anything like this. Matching argument with format specifier
only requires counting items in a list. In most (of my) cases
the list is rather short. When the list get long, I prefer
splitting up into several printf()'s.

I must admit, that even with short lists, errors are sometimes
made easily because of this additional counting (which requires
switching our locus of attention). However, this applies for
many other recurring coding themes. In my view, making special
coding rules is generally speaking, not a good idea: the simpler
the better.

Cheers,

Case

Nov 14 '05 #2
On Sun, 4 Jul 2004, Peter Ammon wrote:
Does anyone ever write the arguments to printf()-like functions under
the corresponding format specifier?

printf("My name is %s and I am %u years old\n",
name, age);

If the arguments are too long, you could put them on the next line

printf("You got %u/%u (%.2f %%) right\n",
numRight,
numTotal,
(100.*numRight)/numTotal);
The intent is to make it easier to match an argument with the
corresponding format specifier. What do you think?


I've never done this myself nor have I even seen it done before. The first
example looks reasonably readable but I find the second example not as
nice.

I've never had a problem with a mismatch of directives to parameters but
if I thought it was a concern I'd be more inclined to do something like:

printf("You got %u/", numRight);
printf("%u (", numTotal);
printf("%.2f %%) right\n", (100.*numRight)/numTotal);

The downside of course is that this is three calls to printf rather than
one. Less efficient. I guess this is why I just do a single line printf
with all three parameters.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
On Sun, 04 Jul 2004 09:18:33 GMT, in comp.lang.c , Peter Ammon
<ge******@mac.com> wrote:
Does anyone ever write the arguments to printf()-like functions under
the corresponding format specifier?
not me.
The intent is to make it easier to match an argument with the
corresponding format specifier. What do you think?


I think it works as long as nobody is using tabs, or everyone has their
tabstop set to the same level. Then it becomes unreadably messy....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #4
"Peter Ammon" <ge******@mac.com> wrote in message
news:JX*****************@newssvr25.news.prodigy.co m...
Does anyone ever write the arguments to printf()-like functions under
the corresponding format specifier?

printf("My name is %s and I am %u years old\n",
name, age);
No, I've never written it that way.
If the arguments are too long, you could put them on the next line

printf("You got %u/%u (%.2f %%) right\n",
numRight,
numTotal,
(100.*numRight)/numTotal);
Yes, when statements become "too long", (any line, not just those
with 'printf()'), I do break them into more than one line. How
many depends upon what I'm breaking up. E.g.

printf("Claimant Full Name: %-50s Social Security Number: %-12s\n",
claimaint, soc_sec_no);
Or if there are "many" data items, I might separate each specifier and
data item on its own line; especially if the output will appear this
way, the statement reflects it:

(I probably wouldn't do this for such a 'small' example as this,
but it shows the format I'd use:)

printf("Name: %s\n"
"Address: %s\n"
"City: %s\n"
"State: %s\n"
"ZIP: %s\n",
name,
address,
city,
state,
zip);
The intent is to make it easier to match an argument with the
corresponding format specifier. What do you think?


I think you should use whichever form you find easiest to use,
but keeping in mind that your attempts at clarity should consider
other readers of your code, not just yourself. Also, many times
you have no choice, but are constrained by 'house' coding standards.

-Mike
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ravi Uday | last post by:
HI, What does these mean - - " lx%08lx " - " %1024%* " I have seen them being used in printfs. When do you exactly write/use these ? Thx in advance
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
5
by: siliconwafer | last post by:
Hi All, What does a 'format specifier' do? Suppose I do, int a = 43; //decimal number printf("%x",a); I will get hex equivalent of 43. Does the format specifier do an implicit "decimal to hex"...
1
by: Chris Morse | last post by:
Hi, I've been trying to figure out where in the documentation it describes all the String.Format() formatting specifiers. So far, I've been guessing and picking up specifiers in sample code.....
6
by: Udi | last post by:
Hi, I tried looking for it in the documentation but with no luck. What's the equivalent C control strings in C# for the following: "%3d" ? (space padding) "%-3d" ? (left alignment) "%+3d" ?...
2
by: rh00667 | last post by:
hi all, seeing a source i found these printf format specifiers (new for me) %m and %n$ i tested and works nice, it seems that they are not documented in man (???) i would appreciate to...
1
by: Ayyanar | last post by:
What does the following format specifiers do fscanf( fd, "%d : \"%\"",..../.);
1
by: (2b|!2b)==? | last post by:
I have the following line in my code, which is supposed to skip commas and white space and to read a comma seperated value string into the appropriate variables: sscanf(temp.c_str(),...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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...

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.