printf() width specifier in text fields

Discussion in 'C Programming' started by MikeC, Jul 22, 2007.

  1. MikeC

    MikeC Guest

    Folks,

    Can the width specifier be used in a printf() text string?

    If I execute...

    printf("%4s", ".........."); it prints ten dots.

    If I execute

    printf("%4s", "."); it prints one dot.

    I was hoping to use sprintf to fill in a number of spaces in a field, such
    as...

    int spaces = 6;

    sprintf(buf, "head %*s tail", spaces, " ");

    If printf() can't do it, I know I can write a function that will handle it,
    but is there a more elegant way? printf() would be attractive because I
    would be able to fill in "head", "tail" and a given number of spaces between
    them, all in one statement.

    Thanks,

    MikeC

    --
    Mental decryption required to bamboozle spam robots:

    mike_best$ntlworld*com
    $ = @
    * = dot
     
    MikeC, Jul 22, 2007
    #1
    1. Advertising

  2. MikeC

    Flash Gordon Guest

    MikeC wrote, On 22/07/07 11:29:
    > Folks,
    >
    > Can the width specifier be used in a printf() text string?
    >
    > If I execute...
    >
    > printf("%4s", ".........."); it prints ten dots.


    printf("%4.4s","..........");

    > If I execute
    >
    > printf("%4s", "."); it prints one dot.


    Are you sure it does not print 3 spaces and 1 dot? Try
    printf('"%4s'", ".");
    so you can see

    > I was hoping to use sprintf to fill in a number of spaces in a field, such
    > as...
    >
    > int spaces = 6;
    >
    > sprintf(buf, "head %*s tail", spaces, " ");


    I'm not entirely sure what you want. Possibly %*.*s specifying minimum
    and maximum length.

    > If printf() can't do it, I know I can write a function that will handle it,
    > but is there a more elegant way? printf() would be attractive because I
    > would be able to fill in "head", "tail" and a given number of spaces between
    > them, all in one statement.


    You can probably do what you want. The number before the decimal point
    is minimum width, after is maximum width, so both the same specifies
    exact width. You can also use negative numbers to specify left justified
    if that is what you want.
    sprintf(buf, "%*.*s %*.*", -7,7,"head",-7,7,"tail");
    Or whatever.
    --
    Flash Gordon
     
    Flash Gordon, Jul 22, 2007
    #2
    1. Advertising

  3. MikeC

    MikeC Guest

    Thanks a million, Flash,

    I learned something I didn't know from your post (that you can specify max
    and min widths), but I independently found the answer to what I was looking
    for by Googling around the web.

    What I was missing was the + or - specifiers in the field. If I execute....

    printf("%4s", "."); it prints a dot (no spaces). If I execute
    printf("%-4s", "."); it prints three spaces and one dot (field width of 4).
    If I execute
    printf("%+4s", "."); it puts the dot at the other end of the field of 4 (a
    dot, then three spaces)
    If I replace the dot with a space, and execute
    printf("%-4s", " "); it gives me three spaces followed by a space, which
    is exactly what I wanted.

    Thanks for your reply all the same - as I said, I learned something!

    MikeC

    "Flash Gordon" <> wrote in message
    news:-gordon.me.uk...
    > MikeC wrote, On 22/07/07 11:29:
    >> Folks,
    >>
    >> Can the width specifier be used in a printf() text string?
    >>
    >> If I execute...
    >>
    >> printf("%4s", ".........."); it prints ten dots.

    >
    > printf("%4.4s","..........");
    >
    >> If I execute
    >>
    >> printf("%4s", "."); it prints one dot.

    >
    > Are you sure it does not print 3 spaces and 1 dot? Try
    > printf('"%4s'", ".");
    > so you can see
    >
    >> I was hoping to use sprintf to fill in a number of spaces in a field,
    >> such as...
    >>
    >> int spaces = 6;
    >>
    >> sprintf(buf, "head %*s tail", spaces, " ");

    >
    > I'm not entirely sure what you want. Possibly %*.*s specifying minimum and
    > maximum length.
    >
    >> If printf() can't do it, I know I can write a function that will handle
    >> it, but is there a more elegant way? printf() would be attractive
    >> because I would be able to fill in "head", "tail" and a given number of
    >> spaces between them, all in one statement.

    >
    > You can probably do what you want. The number before the decimal point is
    > minimum width, after is maximum width, so both the same specifies exact
    > width. You can also use negative numbers to specify left justified if that
    > is what you want.
    > sprintf(buf, "%*.*s %*.*", -7,7,"head",-7,7,"tail");
    > Or whatever.
    > --
    > Flash Gordon
     
    MikeC, Jul 22, 2007
    #3
  4. MikeC

    pete Guest

    MikeC wrote:

    > If I execute....


    > printf("%4s", "."); it prints a dot (no spaces).


    Try it this way and see what happens:

    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    printf("%4s", ".");
    printf("%4s", ".");
    printf("%4s", ".");
    printf("%4s", ".");
    printf("%4s", ".");
    putchar('\n');
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Jul 22, 2007
    #4
  5. MikeC

    MikeC Guest

    Thanks Pete,

    .... but I think you missed the point.

    Regards,

    MikeC

    "pete" <> wrote in message
    news:...
    > MikeC wrote:
    >
    >> If I execute....

    >
    >> printf("%4s", "."); it prints a dot (no spaces).

    >
    > Try it this way and see what happens:
    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > printf("%4s", ".");
    > printf("%4s", ".");
    > printf("%4s", ".");
    > printf("%4s", ".");
    > printf("%4s", ".");
    > putchar('\n');
    > return 0;
    > }
    >
    > /* END new.c */
    >
    > --
    > pete
     
    MikeC, Jul 22, 2007
    #5
  6. MikeC

    pete Guest

    MikeC wrote:
    >
    > Thanks Pete,
    >
    > ... but I think you missed the point.


    I disagree.

    > "pete" <> wrote in message
    > news:...
    > > MikeC wrote:
    > >
    > >> If I execute....

    > >
    > >> printf("%4s", "."); it prints a dot (no spaces).


    What you wrote is wrong:

    > printf("%4s", "."); it prints a dot (no spaces).


    No. It prints 3 spaces followed by a dot.

    > If I execute printf("%-4s", ".");
    > it prints three spaces and one dot (field width of 4).


    No. It prints a dot followed by 3 spaces.

    > If I execute printf("%+4s", ".");
    > it puts the dot at the other end of the field of 4
    > (a dot, then three spaces)


    No. It does the exact same thing as printf("%4s", ".");

    > If I replace the dot with a space, and execute
    > printf("%-4s", " ");
    > it gives me three spaces followed by a space, which
    > is exactly what I wanted.


    It gives you a space followed by three spaces,
    which is what you want,
    but if you knew what you were talking about,
    eventually you would have gotten to this expression:

    printf("%4s", " ")

    Try it again:

    /* BEGIN new.c output */
    .X
    .. X
    .X
    X
    X
    /* END new.c output */


    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    puts("/* BEGIN new.c output */");
    printf("%4s", ".");
    puts("X");
    printf("%-4s", ".");
    puts("X");
    printf("%+4s", ".");
    puts("X");
    printf("%-4s", " ");
    puts("X");
    printf("%4s", " ");
    puts("X");
    puts("/* END new.c output */");
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Jul 22, 2007
    #6
  7. MikeC

    Thad Smith Guest

    MikeC wrote:
    > "pete" <> wrote in message
    > news:...
    >
    >>MikeC wrote:
    >>
    >>> If I execute....
    >>>
    >>>printf("%4s", "."); it prints a dot (no spaces).

    >>
    >>Try it this way and see what happens:
    >>
    >>/* BEGIN new.c */
    >>
    >>#include <stdio.h>
    >>
    >>int main(void)
    >>{
    >> printf("%4s", ".");
    >> printf("%4s", ".");
    >> printf("%4s", ".");
    >> printf("%4s", ".");
    >> printf("%4s", ".");
    >> putchar('\n');
    >> return 0;
    >>}
    >>
    >>/* END new.c */
    >>

    > Thanks Pete,
    >
    > ... but I think you missed the point.


    Pete's point was to correct a misunderstnading you have about printf
    specifiers. Understnnding that, you can get what you want with

    printf ("foo%*sbar", nspaces, "");

    to insert nspaces space characters between "foo" and "bar".

    --
    Thad
     
    Thad Smith, Jul 22, 2007
    #7
    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. AndrewF
    Replies:
    1
    Views:
    786
    Bruce Barker
    Oct 10, 2005
  2. ben
    Replies:
    4
    Views:
    623
    Martin Ambuhl
    Jun 26, 2004
  3. Money
    Replies:
    18
    Views:
    690
    Jack Klein
    Jun 24, 2006
  4. jayapal
    Replies:
    7
    Views:
    633
    Army1987
    Oct 30, 2007
  5. vlsidesign
    Replies:
    2
    Views:
    422
    Keith Thompson
    Nov 14, 2007
Loading...

Share This Page