A question about printf

Discussion in 'C Programming' started by Why Tea, Nov 17, 2006.

  1. Why Tea

    Why Tea Guest

    int printf(const char *fmt,...)

    The fmt is const, indicating that it can't be changed during runtime.
    If I want to print a series of integers that I only know the largest
    value beforehand, instead of testing each range and printf each with
    different width (say 1 space + max number of digits) accordingly, is
    there a better way of doing it?

    /Why Tea
    Why Tea, Nov 17, 2006
    #1
    1. Advertising

  2. Why Tea said:

    > int printf(const char *fmt,...)
    >
    > The fmt is const, indicating that it can't be changed during runtime.


    No, it means printf won't change it.

    Consider this code:

    #include <stdio.h>

    int main(void)
    {
    char format[8] = "%Xs";
    int i = 9;
    while(i > 1)
    {
    format[1] = i-- + '0';
    printf(format, "X\n");
    }
    return 0;
    }

    Perfectly legal C, honest! :)


    > If I want to print a series of integers that I only know the largest
    > value beforehand, instead of testing each range and printf each with
    > different width (say 1 space + max number of digits) accordingly, is
    > there a better way of doing it?


    Yes, but the best answer to your question depends on precisely what you want
    to do. This isn't clear from your question.

    Let's take the following data as being your "series of integers":

    0 1 10 100 1000 10000

    How would you want the output to appear?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: normal service will be restored as soon as possible. Please do not
    adjust your email clients.
    Richard Heathfield, Nov 17, 2006
    #2
    1. Advertising

  3. Why Tea

    Chris Dollin Guest

    Why Tea wrote:

    > int printf(const char *fmt,...)
    >
    > The fmt is const, indicating that it can't be changed during runtime.


    No, the *fmt's are const, indicating that /printf/ won't change them when
    it's called.

    > If I want to print a series of integers that I only know the largest
    > value beforehand, instead of testing each range and printf each with
    > different width (say 1 space + max number of digits) accordingly, is
    > there a better way of doing it?


    I don't understand what you're asking for. Examples? And have you
    read up on what the printf format options are -- including the
    * magic number?

    --
    Chris "hantwig efferko VOOM!" Dollin
    "- born in the lab under strict supervision -", - Magenta, /Genetesis/
    Chris Dollin, Nov 17, 2006
    #3
  4. Why Tea

    Why Tea Guest

    Richard Heathfield wrote:
    > Why Tea said:
    >
    > > int printf(const char *fmt,...)
    > >
    > > The fmt is const, indicating that it can't be changed during runtime.

    >
    > No, it means printf won't change it.
    >
    > Consider this code:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > char format[8] = "%Xs";
    > int i = 9;
    > while(i > 1)
    > {
    > format[1] = i-- + '0';
    > printf(format, "X\n");
    > }
    > return 0;
    > }
    >
    > Perfectly legal C, honest! :)
    >
    >
    > > If I want to print a series of integers that I only know the largest
    > > value beforehand, instead of testing each range and printf each with
    > > different width (say 1 space + max number of digits) accordingly, is
    > > there a better way of doing it?

    >
    > Yes, but the best answer to your question depends on precisely what you want
    > to do. This isn't clear from your question.
    >
    > Let's take the following data as being your "series of integers":
    >
    > 0 1 10 100 1000 10000
    >
    > How would you want the output to appear?


    Thanks Richard. You have actually answered my question. But I will
    still give you an example and I would like to see your solution :)

    1) 1, 3 , 4, 12, 56, 67 => 001, 003, 004, 056, 067
    2) 1, 3, 4, 112 => 0001, 0003, 0004, 0012
    3) 1, 3, 4, 1122 => 00001, 00003, 00004, 01122

    We can also assume the numbers are store in an int array.
    Why Tea, Nov 17, 2006
    #4
  5. Why Tea said:

    <snip>
    >
    > Thanks Richard. You have actually answered my question. But I will
    > still give you an example and I would like to see your solution :)
    >
    > 1) 1, 3 , 4, 12, 56, 67 => 001, 003, 004, 056, 067
    > 2) 1, 3, 4, 112 => 0001, 0003, 0004, 0012
    > 3) 1, 3, 4, 1122 => 00001, 00003, 00004, 01122
    >
    > We can also assume the numbers are store in an int array.


    You also said you knew the largest number. So the next step is to calculate
    how many digits it has. Once you've done that:

    for(i = 0; i < n; i++)
    {
    printf("%0*d\n", digits + 1, data[n]);
    }

    The 0 means "pad to the left with 0s".
    The * means "make this field as wide as... well, read a parameter to find
    out, okay?" (I was amazed and delighted when I first discovered this
    feature of printf.)
    The d, of course, you know already.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: normal service will be restored as soon as possible. Please do not
    adjust your email clients.
    Richard Heathfield, Nov 17, 2006
    #5
  6. Why Tea

    Why Tea Guest

    >
    > for(i = 0; i < n; i++)
    > {
    > printf("%0*d\n", digits + 1, data[n]);
    > }
    >
    > The 0 means "pad to the left with 0s".
    > The * means "make this field as wide as... well, read a parameter to find
    > out, okay?" (I was amazed and delighted when I first discovered this
    > feature of printf.)


    I know how you felt. I've just experienced that. Thanks for sharing!
    Why Tea, Nov 17, 2006
    #6
    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. Edith Gross
    Replies:
    2
    Views:
    329
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,223
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    423
  5. guru
    Replies:
    8
    Views:
    264
Loading...

Share This Page