Writing arguments under format specifiers

Discussion in 'C Programming' started by Peter Ammon, Jul 4, 2004.

  1. Peter Ammon

    Peter Ammon Guest

    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.
     
    Peter Ammon, Jul 4, 2004
    #1
    1. Advertising

  2. Peter Ammon

    Case - Guest

    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
     
    Case -, Jul 4, 2004
    #2
    1. Advertising

  3. 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
     
    Darrell Grainger, Jul 5, 2004
    #3
  4. On Sun, 04 Jul 2004 09:18:33 GMT, in comp.lang.c , Peter Ammon
    <> 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 =---
     
    Mark McIntyre, Jul 5, 2004
    #4
  5. Peter Ammon

    Mike Wahler Guest

    "Peter Ammon" <> wrote in message
    news:JXPFc.6155$...
    > 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
     
    Mike Wahler, Jul 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    0
    Views:
    656
  2. Ben Pfaff

    Re: C# format Specifiers Help.

    Ben Pfaff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    417
    Ben Pfaff
    Aug 21, 2003
  3. Martin Ambuhl

    Re: C# format Specifiers Help.

    Martin Ambuhl, Aug 21, 2003, in forum: C Programming
    Replies:
    4
    Views:
    514
    Randy Howard
    Aug 22, 2003
  4. Jeff

    Re: C# format Specifiers Help.

    Jeff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    476
  5. siliconwafer

    understanding format specifiers

    siliconwafer, Aug 30, 2005, in forum: C Programming
    Replies:
    5
    Views:
    813
    Charlie Gordon
    Aug 31, 2005
Loading...

Share This Page