variables in format strings -- printf

Discussion in 'C Programming' started by Adrian Neumeyer, Sep 16, 2003.

  1. Hello folks,

    I have a simple question:

    I want to control the indentation of strings printed out
    with printf(...). Not just a fixed indentation, but lets say
    one blank character more for each new line, so that
    a staircase effect is produced.

    Example:
    One
    Two
    Three
    ....

    I was not able to create a working format string for
    printf().

    Can somebody help me?

    Thanks!

    Adrian
    Adrian Neumeyer, Sep 16, 2003
    #1
    1. Advertising

  2. "Adrian Neumeyer" <> writes:

    > I want to control the indentation of strings printed out
    > with printf(...). Not just a fixed indentation, but lets say
    > one blank character more for each new line, so that
    > a staircase effect is produced.
    >
    > Example:
    > One
    > Two
    > Three
    > ....
    >
    > I was not able to create a working format string for
    > printf().


    #include <stdio.h>

    int main (void)
    {
    const char *const strings [] = {"One", "Two", "Three"};
    size_t i;

    for (i = 0; i < sizeof strings / sizeof strings [0]; ++i)
    printf ("%*s%s\n", (int)i, "", strings );

    return 0;
    }

    "%*s" expects two arguments, an `int' and a pointer to a string. The
    former specifies the minimum width for the latter. The trick here is
    to output an empty string, so that only padding spaces are written.

    Martin
    Martin Dickopp, Sep 16, 2003
    #2
    1. Advertising

  3. Adrian Neumeyer

    nrk Guest

    Adrian Neumeyer wrote:

    > Hello folks,
    >
    > I have a simple question:
    >
    > I want to control the indentation of strings printed out
    > with printf(...). Not just a fixed indentation, but lets say
    > one blank character more for each new line, so that
    > a staircase effect is produced.
    >
    > Example:
    > One
    > Two
    > Three
    > ....
    >
    > I was not able to create a working format string for
    > printf().
    >


    printf("%*s\n", width, string);

    where width is the field width of the string printed, and can be computed
    using the length of the string to be printed and whatever lead space you
    want for your staircase effect.

    HTH,
    nrk.

    > Can somebody help me?
    >
    > Thanks!
    >
    > Adrian
    nrk, Sep 16, 2003
    #3
  4. Adrian Neumeyer

    Joe Wright Guest

    Adrian Neumeyer wrote:
    >
    > Hello folks,
    >
    > I have a simple question:
    >
    > I want to control the indentation of strings printed out
    > with printf(...). Not just a fixed indentation, but lets say
    > one blank character more for each new line, so that
    > a staircase effect is produced.
    >
    > Example:
    > One
    > Two
    > Three
    > ....
    >
    > I was not able to create a working format string for
    > printf().
    >
    > Can somebody help me?
    >
    > Thanks!
    >
    > Adrian

    #include <stdio.h>

    int main(void) {
    int i;
    for (i = 0; i < 5; ++i)
    printf("%*s%s\n", i, "", "Adrian");
    return 0;
    }

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Sep 16, 2003
    #4
  5. Adrian Neumeyer wrote:


    > I want to control the indentation of strings printed out
    > with printf(...). Not just a fixed indentation, but lets say
    > one blank character more for each new line, so that
    > a staircase effect is produced.
    >
    > Example:
    > One
    > Two
    > Three
    > ....
    >
    > I was not able to create a working format string for
    > printf().


    printf("%*s%s\n",currentindent,"",texttoprint);


    --
    Martin Ambuhl
    Martin Ambuhl, Sep 16, 2003
    #5
  6. Adrian Neumeyer

    Thomas Pfaff Guest

    Martin Dickopp <> writes:
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > const char *const strings [] = {"One", "Two", "Three"};


    Note that the identifiers str[a-z]* is reserved for use by the
    implementation, thus the name `strings' violate the Standard.
    Thomas Pfaff, Sep 16, 2003
    #6
  7. Adrian Neumeyer

    Thomas Pfaff Guest

    Thomas Pfaff <> writes, again and again:

    > Martin Dickopp <> writes:
    > > #include <stdio.h>
    > >
    > > int main (void)
    > > {
    > > const char *const strings [] = {"One", "Two", "Three"};

    >
    > Note that the identifiers str[a-z]* is reserved for use by the
    > implementation, thus the name `strings' violate the Standard.


    Or wait, did that only apply to identifiers with external linkage?
    Thomas Pfaff, Sep 16, 2003
    #7
  8. Thomas Pfaff <> wrote:

    >Thomas Pfaff <> writes, again and again:
    >
    >> Martin Dickopp <> writes:
    >> > #include <stdio.h>
    >> >
    >> > int main (void)
    >> > {
    >> > const char *const strings [] = {"One", "Two", "Three"};

    >>
    >> Note that the identifiers str[a-z]* is reserved for use by the
    >> implementation, thus the name `strings' violate the Standard.

    >
    >Or wait, did that only apply to identifiers with external linkage?

    Yes.

    From n48:

    7.26 Future library directions

    [#1] [...] All external names described below
    are reserved no matter what headers are included by the
    program.


    7.26.11 String handling <string.h>

    [#1] Function names that begin with str, mem, or wcs and a
    lowercase letter (possibly followed by any combination of
    digits, letters, and underscore) may be added to the
    declarations in the <string.h> header.

    Regards

    Irrwahn
    --
    What does this red button do?
    Irrwahn Grausewitz, Sep 16, 2003
    #8
  9. On Tue, 16 Sep 2003 10:28:32 +0200, Irrwahn Grausewitz
    <> wrote:

    > Thomas Pfaff <> wrote:
    >
    > >Thomas Pfaff <> writes, again and again:
    > >
    > >> Martin Dickopp <> writes:
    > >> > #include <stdio.h>
    > >> >
    > >> > int main (void)
    > >> > {
    > >> > const char *const strings [] = {"One", "Two", "Three"};
    > >>
    > >> Note that the identifiers str[a-z]* is reserved for use by the
    > >> implementation, thus the name `strings' violate the Standard.

    > >
    > >Or wait, did that only apply to identifiers with external linkage?


    > Yes.
    >
    > From n48:
    >

    IGYM n843, and even if you can't/won't get the actual standard you
    might as well move up to n869, it's equally free.

    > 7.26 Future library directions
    >
    > [#1] [...] All external names described below
    > are reserved no matter what headers are included by the
    > program.
    >

    And a local (ordinary) identifier does not conflict with an external
    name, so it's OK on this front.

    > 7.26.11 String handling <string.h>
    >
    > [#1] Function names that begin with str, mem, or wcs and a
    > lowercase letter (possibly followed by any combination of
    > digits, letters, and underscore) may be added to the
    > declarations in the <string.h> header.
    >

    And all library functions can be macros, so this effectively reserves
    the names for any use, *if* you don't explicitly #undef after
    #include'ing <string.h>, which the example didn't -- but in a real
    program might be hidden in another header or added at any time, so to
    be absolutely safe you should just steer clear.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Sep 22, 2003
    #9
  10. <grab> Added to the list of interview questions </grab>

    Thanks!


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Per the FCA, this address may not be added to any commercial mail list
    Kevin D. Quitt, Sep 22, 2003
    #10
    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. Richard Bos

    printf with run-time format strings

    Richard Bos, Feb 10, 2004, in forum: C Programming
    Replies:
    11
    Views:
    628
    Chris Torek
    Feb 13, 2004
  2. ben
    Replies:
    4
    Views:
    596
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,213
  4. dank
    Replies:
    5
    Views:
    1,459
    Simon Biber
    Jun 30, 2006
  5. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    418
Loading...

Share This Page