fprintf formatting question???

Discussion in 'C Programming' started by jchludzinski@gmail.com, May 20, 2005.

  1. Guest

    I tryin' to print out a string with a variable number of blanks/spaces
    preceding the string. I know that if I use:

    fprintf( stdout, "%12s", string );

    I get 12 blanks preceding 'string'. If I use:

    n = 12;
    fprintf( stdout, "%(n)s: ", string );

    I get: %(n)s:

    Any ideas?

    ---John
    , May 20, 2005
    #1
    1. Advertising

  2. kyle york Guest

    Greetings,

    wrote:
    > I tryin' to print out a string with a variable number of blanks/spaces
    > preceding the string. I know that if I use:
    >
    > fprintf( stdout, "%12s", string );
    >
    > I get 12 blanks preceding 'string'. If I use:
    >
    > n = 12;
    > fprintf( stdout, "%(n)s: ", string );


    fprintf( stdout, "%*s: ", n, string);



    --
    Kyle A. York
    Sr. Subordinate Grunt
    kyle york, May 20, 2005
    #2
    1. Advertising

  3. Ben Pfaff Guest

    "" <> writes:

    > I tryin' to print out a string with a variable number of blanks/spaces
    > preceding the string. I know that if I use:
    >
    > fprintf( stdout, "%12s", string );
    >
    > I get 12 blanks preceding 'string'. If I use:
    >
    > n = 12;
    > fprintf( stdout, "%(n)s: ", string );


    printf("%*s: ", n, string);
    --
    "When in doubt, treat ``feature'' as a pejorative.
    (Think of a hundred-bladed Swiss army knife.)"
    --Kernighan and Plauger, _Software Tools_
    Ben Pfaff, May 20, 2005
    #3
  4. Guest

    Thanks much! ---John
    , May 20, 2005
    #4
  5. On Fri, 20 May 2005 12:45:42 -0700, kyle york wrote:

    > Greetings,
    >
    > wrote:
    >> I tryin' to print out a string with a variable number of blanks/spaces
    >> preceding the string. I know that if I use:
    >>
    >> fprintf( stdout, "%12s", string );
    >>
    >> I get 12 blanks preceding 'string'. If I use:


    No, you will get leading blanks added to make the total length of the
    field up to 12. You would only get 12 blanks added if string has zero
    length.

    >>
    >> n = 12;
    >> fprintf( stdout, "%(n)s: ", string );

    >
    > fprintf( stdout, "%*s: ", n, string);


    Note that the argument passed for * muts have type int. Cast if n has some
    other type.

    Lawrence
    Lawrence Kirby, May 21, 2005
    #5
  6. Guest

    Strange thing happened on the way to the forum:

    fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
    get_entity_id( child, level+1 ) );

    produces no leading blanks (spaces),

    while:

    fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
    attr );

    produces the expected/require leading blanks (spaces).

    I checked the value of 'level' in the first fprintf and it was what I
    expected (3). One other observation: if I replace the first fprintf
    statement with the second (in the same routine) - it works as expects:
    it adds the leading spaces.

    Any thoughts?
    , May 24, 2005
    #6
  7. Chris Torek Guest

    In article <>
    <> wrote:
    >fprintf( stderr, "%*s %s: \n", 6*level, "Target Entity ID: ",
    >get_entity_id( child, level+1 ) );


    Note: strlen("Target Entity ID: ") is 18.

    >produces no leading blanks (spaces), while:
    >
    >fprintf( stderr, "%*s %s name: %s: \n", 6*level, "Node: ", child->name,
    >attr );
    >
    >produces the expected/require leading blanks (spaces).


    Note: strlen("Node: ") is 6.

    Next, we have:

    >I checked the value of 'level' in the first fprintf and it was what I
    >expected (3).


    Assuming "level" has type int (or promotes to type int), 6*level will
    be 6*3, i.e., the "int" value 18.

    printf("[%18s]", "123456789012345678");
    printf("[%18s]", "123456");

    should cause the square brackets to line up, because the first
    string is 18 characters long and printed in an 18 character field,
    while the second string is 6 characters long and printed in an 18
    character field. The field width tells printf() to pad (with
    spaces, in this case) as needed to make sure the output is at least
    that long. Since 18 minus 18 is zero, the first printf() requires
    no extra padding characters.

    (If "level" has type long, or double, or similar, all bets are off,
    because "%*s" needs one (int) and one (char *). I suspect this is
    not a problem, though.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, May 24, 2005
    #7
  8. Guest

    Got it, thanks! ---John
    , May 24, 2005
    #8
    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. Koen
    Replies:
    1
    Views:
    584
    Victor Bazarov
    Oct 5, 2004
  2. Replies:
    2
    Views:
    431
    Barry Schwarz
    Jan 2, 2004
  3. Andrew Fabbro

    fprintf/variable args question

    Andrew Fabbro, Jun 16, 2004, in forum: C Programming
    Replies:
    3
    Views:
    349
    Andrew Fabbro
    Jun 17, 2004
  4. GGarramuno

    Simple fprintf question

    GGarramuno, Aug 2, 2004, in forum: C Programming
    Replies:
    4
    Views:
    345
    CBFalconer
    Aug 2, 2004
  5. David Mathog
    Replies:
    13
    Views:
    1,106
    J. J. Farrell
    Nov 29, 2007
Loading...

Share This Page