printf format string: how bad style is this? 
Author Message
 printf format string: how bad style is this?

Hi,

I've got a few functions (similar to f() in the program below), all
containing calls to printf(). The functions themselves are different,
but the printf() format string should be the same in all of them. Now,
is it okay to just #define the format string as in the program below, or
should I create a variable and pass this to the functions in question as
an extra parameter?

/* this program just prints garbage */
#include <stdio.h>

#define FORMAT "% 5.3f "
void f(double *arr, int len)
{
        int i;
        for (i = 0; i < len; i++) {
                printf(FORMAT, arr[i]);
        }
        putchar('\n');

Quote:
}

int main(void)
{
        double a[10];
        f(a, 10);
        return 0;

Quote:
}

Stig
--
brautaset.org


Sat, 26 Mar 2005 23:29:01 GMT  
 printf format string: how bad style is this?


Quote:
> I've got a few functions (similar to f() in the program below), all
> containing calls to printf(). The functions themselves are different,
> but the printf() format string should be the same in all of them. Now,
> is it okay to just #define the format string as in the program below, or
> should I create a variable and pass this to the functions in question as
> an extra parameter?

That's a matter of opinion. I think it's fine with the macro


Sun, 27 Mar 2005 00:30:03 GMT  
 printf format string: how bad style is this?

Quote:

> I've got a few functions (similar to f() in the program below),
> all containing calls to printf(). The functions themselves are
> different, but the printf() format string should be the same in
> all of them. Now, is it okay to just #define the format string
> as in the program below, or should I create a variable and pass
> this to the functions in question as an extra parameter?

> /* this program just prints garbage */
> #include <stdio.h>

> #define FORMAT "% 5.3f "
> void f(double *arr, int len)
> {
>         int i;
>         for (i = 0; i < len; i++) {
>                 printf(FORMAT, arr[i]);
>         }
>         putchar('\n');
> }

> int main(void)
> {
>         double a[10];
>         f(a, 10);
>         return 0;
> }

Of course it does.  The contents of a are uninitialized.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 27 Mar 2005 00:45:23 GMT  
 printf format string: how bad style is this?
i would just ditch the #define statement all together. Do you thin that
FORMAT will ever change? If no, then ditch it. Maybe you have bigger plans
FORMAT... I find that using too many #defines/consts can make your code MORE
confusing. It may be obvious what you mean by FORMAT today, but what if dig
this code a few months from now, years? What if somebody else takes over the
project?

- nevry.

Quote:

> Hi,

> I've got a few functions (similar to f() in the program below), all
> containing calls to printf(). The functions themselves are different,
> but the printf() format string should be the same in all of them. Now,
> is it okay to just #define the format string as in the program below, or
> should I create a variable and pass this to the functions in question as
> an extra parameter?

> /* this program just prints garbage */
> #include <stdio.h>

> #define FORMAT "% 5.3f "
> void f(double *arr, int len)
> {
> int i;
> for (i = 0; i < len; i++) {
> printf(FORMAT, arr[i]);
> }
> putchar('\n');
> }

> int main(void)
> {
> double a[10];
> f(a, 10);
> return 0;
> }

> Stig
> --
> brautaset.org



Sun, 27 Mar 2005 00:46:29 GMT  
 printf format string: how bad style is this?

Quote:

>i would just ditch the #define statement all together. Do you thin that
>FORMAT will ever change? If no, then ditch it. Maybe you have bigger plans
>FORMAT... I find that using too many #defines/consts can make your code MORE
>confusing. It may be obvious what you mean by FORMAT today, but what if dig
>this code a few months from now, years? What if somebody else takes over the
>project?

(BTW, please don't top-post.)

Gotta disagree: this seems like a perfect place for a macro, to me.
A number of functions, which all must have the same output format.
What if you decide to change the format?  Perhaps a different number
of decimal places.  Or perhaps "%+5.3f " instead of "% 5.3f".  If
you had this hard-coded all over the place, how likely is it that
you might miss one (especially if you didn't write it originally,
and someone just told you to go in and change the output format)?
Placed in a shared .h file, the macro can also facilitate "symbiosis"
between the producer of these strings, and a downstream consumer:
the consumer can look at FORMAT to tell exactly what to expect and
how to parse it, and when it changes, everything will stay in step.

--Ben

Quote:
>- nevry.


>> Hi,

>> I've got a few functions (similar to f() in the program below), all
>> containing calls to printf(). The functions themselves are different,
>> but the printf() format string should be the same in all of them. Now,
>> is it okay to just #define the format string as in the program below, or
>> should I create a variable and pass this to the functions in question as
>> an extra parameter?

>> /* this program just prints garbage */
>> #include <stdio.h>

>> #define FORMAT "% 5.3f "
>> void f(double *arr, int len)
>> {
>> int i;
>> for (i = 0; i < len; i++) {
>> printf(FORMAT, arr[i]);
>> }
>> putchar('\n');
>> }

>> int main(void)
>> {
>> double a[10];
>> f(a, 10);
>> return 0;
>> }

>> Stig
>> --
>> brautaset.org

--


Sun, 27 Mar 2005 01:53:20 GMT  
 printf format string: how bad style is this?

Quote:

> Hi,

> I've got a few functions (similar to f() in the program below), all
> containing calls to printf(). The functions themselves are different,
> but the printf() format string should be the same in all of them. Now,
> is it okay to just #define the format string as in the program below, or
> should I create a variable and pass this to the functions in question as
> an extra parameter?

> /* this program just prints garbage */
> #include <stdio.h>

> #define FORMAT "% 5.3f "
> void f(double *arr, int len)
> {
> int i;
> for (i = 0; i < len; i++) {
> printf(FORMAT, arr[i]);
> }
> putchar('\n');
> }

> int main(void)
> {
> double a[10];
> f(a, 10);
> return 0;
> }

> Stig

How about creating a new function, e.g. print_formatted() and use the format
string in one place only? Something like

static inline int print_formatted(double d)
{
        return printf("%5.3f ", d);

Quote:
}

Bj?rn


Sun, 27 Mar 2005 02:20:53 GMT  
 printf format string: how bad style is this?


Quote:


> >i would just ditch the #define statement all together. Do you thin that
> >FORMAT will ever change? If no, then ditch it. Maybe you have bigger
plans
> >FORMAT... I find that using too many #defines/consts can make your code
MORE
> >confusing. It may be obvious what you mean by FORMAT today, but what if
dig
> >this code a few months from now, years? What if somebody else takes over
the
> >project?

> (BTW, please don't top-post.)

> Gotta disagree: this seems like a perfect place for a macro, to me.
> A number of functions, which all must have the same output format.
> What if you decide to change the format?  Perhaps a different number
> of decimal places.  Or perhaps "%+5.3f " instead of "% 5.3f".  If
> you had this hard-coded all over the place, how likely is it that
> you might miss one (especially if you didn't write it originally,
> and someone just told you to go in and change the output format)?
> Placed in a shared .h file, the macro can also facilitate "symbiosis"
> between the producer of these strings, and a downstream consumer:
> the consumer can look at FORMAT to tell exactly what to expect and
> how to parse it, and when it changes, everything will stay in step.

> --Ben

> >- nevry.


> >> Hi,

> >> I've got a few functions (similar to f() in the program below), all
> >> containing calls to printf(). The functions themselves are different,
> >> but the printf() format string should be the same in all of them. Now,
> >> is it okay to just #define the format string as in the program below,
or
> >> should I create a variable and pass this to the functions in question
as
> >> an extra parameter?

> >> /* this program just prints garbage */
> >> #include <stdio.h>

> >> #define FORMAT "% 5.3f "
> >> void f(double *arr, int len)
> >> {
> >> int i;
> >> for (i = 0; i < len; i++) {
> >> printf(FORMAT, arr[i]);
> >> }
> >> putchar('\n');
> >> }

> >> int main(void)
> >> {
> >> double a[10];
> >> f(a, 10);
> >> return 0;
> >> }

> >> Stig
> >> --
> >> brautaset.org

> --

I don't think either of us has enough information make a judgement. If
FORMAT is used more than once or twice, then it's probobly a good idea to
use it(i somehow missed him/her saying that...). If not, then it's probobly
a waste of coding. On the other hand, if your coding team really likes
macros, then use them, whatever you fell most comfortable with. The big
question here is; will FORMAT ever change?
Hint: Be careful to document what your macros mean, and where they are or
might be used--even if you are a one-(wo)man 'team'.

- nevry.



Sun, 27 Mar 2005 05:28:41 GMT  
 printf format string: how bad style is this?

Quote:



>>i would just ditch the #define statement all together. Do you thin that
>>FORMAT will ever change? If no, then ditch it. Maybe you have bigger plans
>>FORMAT... I find that using too many #defines/consts can make your code MORE
>>confusing. It may be obvious what you mean by FORMAT today, but what if dig
>>this code a few months from now, years? What if somebody else takes over the
>>project?

> (BTW, please don't top-post.)

> Gotta disagree: this seems like a perfect place for a macro, to me.
> A number of functions, which all must have the same output format.
> What if you decide to change the format?  Perhaps a different number
> of decimal places.  Or perhaps "%+5.3f " instead of "% 5.3f".  If
> you had this hard-coded all over the place, how likely is it that
> you might miss one (especially if you didn't write it originally,
> and someone just told you to go in and change the output format)?
> Placed in a shared .h file, the macro can also facilitate "symbiosis"
> between the producer of these strings, and a downstream consumer:
> the consumer can look at FORMAT to tell exactly what to expect and
> how to parse it, and when it changes, everything will stay in step.

That was my idea. I'm not quite sure how much precision I need just yet,
but this way it's easy to just print low-precision numbers for easy
overview while debugging, and turning the precision way up when in
"production". The code is for a coursework at uni, so it'll most likely
just be thrown away after hand-in, however I was just asking for the
general case.

Stig
--
brautaset.org



Sun, 27 Mar 2005 05:47:31 GMT  
 printf format string: how bad style is this?

Quote:

>> >> I've got a few functions (similar to f() in the program below),
>> >> all containing calls to printf(). The functions themselves are
>> >> different, but the printf() format string should be the same in
>> >> all of them. Now, is it okay to just #define the format string as
>> >> in the program below, or should I create a variable and pass this
>> >> to the functions in question as an extra parameter?
>> >i would just ditch the #define statement all together. Do you thin
>> >that FORMAT will ever change? If no, then ditch it. Maybe you have
>> >bigger plans FORMAT... I find that using too many #defines/consts
>> >can make your code MORE confusing. It may be obvious what you mean
>> >by FORMAT today, but what if dig this code a few months from now,
>> >years? What if somebody else takes over the project?
>> (BTW, please don't top-post.)

>> Gotta disagree: this seems like a perfect place for a macro, to me.
>> A number of functions, which all must have the same output format.
>> What if you decide to change the format?  Perhaps a different number
> I don't think either of us has enough information make a judgement. If
> FORMAT is used more than once or twice, then it's probobly a good idea
> to use it(i somehow missed him/her saying that...).

FORMAT is used more than once, and is likely to change, yes.
Oh, and I'm a 'he' :)

Stig
--
brautaset.org



Sun, 27 Mar 2005 05:52:38 GMT  
 printf format string: how bad style is this?

Quote:

> Hi,

> I've got a few functions (similar to f() in the program below), all
> containing calls to printf(). The functions themselves are different,
> but the printf() format string should be the same in all of them. Now,
> is it okay to just #define the format string as in the program below, or
> should I create a variable and pass this to the functions in question as
> an extra parameter?

> /* this program just prints garbage */
> #include <stdio.h>

> #define FORMAT "% 5.3f "
> void f(double *arr, int len)
> {
>    int i;
>    for (i = 0; i < len; i++) {
>            printf(FORMAT, arr[i]);
>    }
>  putchar('\n');
> }

> int main(void)
> {
>    double a[10];
>    f(a, 10);
>    return 0;
> }

I tend to do

static const char FORMAT[] = "% 5.3f ";

mainly because I suffered some early compilers which would create n
copies of "% 5.3f ", even though you weren't modifying any of them
[one even created a string for sizeof("% 5.3f ")!]. Sensible compilers
know (or can be told) to create only one copy.

Chris



Sun, 27 Mar 2005 07:36:00 GMT  
 printf format string: how bad style is this?


Quote:
> I tend to do

> static const char FORMAT[] = "% 5.3f ";

> mainly because I suffered some early compilers which would create n
> copies of "% 5.3f ", even though you weren't modifying any of them
> [one even created a string for sizeof("% 5.3f ")!]. Sensible compilers
> know (or can be told) to create only one copy.

I think you're probably on safe ground with string literals now :) C99's
compound literals re-introduce the problem though. I'd be interested to know
what other compilers' approaches have been. In my implementation

       void Draw(Point);

       Draw((Point) { 1, 2 });
       Draw((Point) { 2, 3 });
       Draw((Point) { 1, 2 });

would create 2 copies of (Point) { 1, 2 }, but

       Draw((const Point) { 1, 2 });
       Draw((const Point) { 2, 3 });
       Draw((const Point) { 1, 2 });

will only create 1 copy of (const Point) { 1, 2 }.

Now a brighter compiler shouldn't need the const to avoid the duplicate
literal, but I'm not sure how many, if any, are bright enough here. In this
case, an even brighter compiler wouldn't allocate a Point object - it'd just
pass the right values to the function directly, just like passing a scalar.

(As an aside, your sizeof example just filled me with dread, but I've checked
and I'm not actually creating a compound literal for that case. Phew.)

--
Kevin Bracey
http://www.bracey-griffith.freeserve.co.uk/



Sun, 27 Mar 2005 17:41:45 GMT  
 printf format string: how bad style is this?

Quote:
>That was my idea. I'm not quite sure how much precision I need just yet,
>but this way it's easy to just print low-precision numbers for easy
>overview while debugging, and turning the precision way up when in
>"production". The code is for a coursework at uni, so it'll most likely
>just be thrown away after hand-in, however I was just asking for the
>general case.

If it's only the precision that it's likely to change, it's probably
better to use a macro only for the precision, and use the * feature
to specify the precision.  This way, you can have the same precision
everywhere (you need), even if the format strings are otherwise different.

OTOH, if you *need* to have the identical format string everywhere and
any part of it is subject to later changes, either use your approach
or define a macro for the whole printf call.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 27 Mar 2005 18:28:09 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Format string as a parameter to printf()?

2. printf -*s format string

3. Examples of format strings for scanf/printf ?

4. printf and formatting numeric strings

5. placing output of printf in memory: formatting strings

6. printf and variable length string format (asterisk)

7. Printf format strings

8. printf() format-string parser wanted!

9. printf Format string

10. Source to format strings like printf()

11. STL strings & printf-type formatting

12. Worse than the PRINTF statement...

 

 
Powered by phpBB® Forum Software