variables in format strings -- printf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian Neumeyer

    variables in format strings -- printf

    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


  • Martin Dickopp

    #2
    Re: variables in format strings -- printf

    "Adrian Neumeyer" <aneumeyer@yaho o.de> writes:
    [color=blue]
    > 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().[/color]

    #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 [i]);

    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

    Comment

    • Joe Wright

      #3
      Re: variables in format strings -- printf

      Adrian Neumeyer wrote:[color=blue]
      >
      > 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[/color]
      #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:joewwrig ht@earthlink.ne t
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • Martin Ambuhl

        #4
        Re: variables in format strings -- printf

        Adrian Neumeyer wrote:

        [color=blue]
        > 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().[/color]

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


        --
        Martin Ambuhl

        Comment

        • nrk

          #5
          Re: variables in format strings -- printf

          Adrian Neumeyer wrote:
          [color=blue]
          > 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().
          >[/color]

          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.
          [color=blue]
          > Can somebody help me?
          >
          > Thanks!
          >
          > Adrian[/color]

          Comment

          • Thomas Pfaff

            #6
            Re: variables in format strings -- printf

            Martin Dickopp <expires-nov2003@zero-based.org> writes:[color=blue]
            > #include <stdio.h>
            >
            > int main (void)
            > {
            > const char *const strings [] = {"One", "Two", "Three"};[/color]

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

            Comment

            • Thomas Pfaff

              #7
              Re: variables in format strings -- printf

              Thomas Pfaff <thomas.pfaff@t iscali.no> writes, again and again:
              [color=blue]
              > Martin Dickopp <expires-nov2003@zero-based.org> writes:[color=green]
              > > #include <stdio.h>
              > >
              > > int main (void)
              > > {
              > > const char *const strings [] = {"One", "Two", "Three"};[/color]
              >
              > Note that the identifiers str[a-z]* is reserved for use by the
              > implementation, thus the name `strings' violate the Standard.[/color]

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

              Comment

              • Irrwahn Grausewitz

                #8
                Re: variables in format strings -- printf

                Thomas Pfaff <thomas.pfaff@t iscali.no> wrote:
                [color=blue]
                >Thomas Pfaff <thomas.pfaff@t iscali.no> writes, again and again:
                >[color=green]
                >> Martin Dickopp <expires-nov2003@zero-based.org> writes:[color=darkred]
                >> > #include <stdio.h>
                >> >
                >> > int main (void)
                >> > {
                >> > const char *const strings [] = {"One", "Two", "Three"};[/color]
                >>
                >> Note that the identifiers str[a-z]* is reserved for use by the
                >> implementation, thus the name `strings' violate the Standard.[/color]
                >
                >Or wait, did that only apply to identifiers with external linkage?[/color]
                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?

                Comment

                • Dave Thompson

                  #9
                  Re: variables in format strings -- printf

                  On Tue, 16 Sep 2003 10:28:32 +0200, Irrwahn Grausewitz
                  <irrwahn@freene t.de> wrote:
                  [color=blue]
                  > Thomas Pfaff <thomas.pfaff@t iscali.no> wrote:
                  >[color=green]
                  > >Thomas Pfaff <thomas.pfaff@t iscali.no> writes, again and again:
                  > >[color=darkred]
                  > >> Martin Dickopp <expires-nov2003@zero-based.org> 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.[/color]
                  > >
                  > >Or wait, did that only apply to identifiers with external linkage?[/color][/color]
                  [color=blue]
                  > Yes.
                  >
                  > From n48:
                  >[/color]
                  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.
                  [color=blue]
                  > 7.26 Future library directions
                  >
                  > [#1] [...] All external names described below
                  > are reserved no matter what headers are included by the
                  > program.
                  >[/color]
                  And a local (ordinary) identifier does not conflict with an external
                  name, so it's OK on this front.
                  [color=blue]
                  > 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.
                  >[/color]
                  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.ne t

                  Comment

                  • Kevin D. Quitt

                    #10
                    Re: variables in format strings -- printf

                    <grab> Added to the list of interview questions </grab>

                    Thanks!


                    --
                    #include <standard.discl aimer>
                    _
                    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

                    Comment

                    Working...