Writing arguments under format specifiers

P

Peter Ammon

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
 
C

Case -

Peter said:
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
 
D

Darrell Grainger

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.
 
M

Mark McIntyre

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....
 
M

Mike Wahler

Peter Ammon said:
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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top