Skipping parameters in a printf()

Discussion in 'C Programming' started by pozz, May 2, 2007.

  1. pozz

    pozz Guest

    Is there some modifier that skips a parameter of a printf?

    For example, I pass three parameters:
    printf( <format string>, '-', value/10, value%10 );

    In certain cases I want to print the minus sign character, in other
    cases I want to skip it.
    If I want to print the sign, it is very simple:
    <format string> = "%c%d.%1d"
    What about if I want to print only the last two parameters of
    printf(), skipping the sign character?
    pozz, May 2, 2007
    #1
    1. Advertising

  2. pozz said:

    > Is there some modifier that skips a parameter of a printf?
    >
    > For example, I pass three parameters:
    > printf( <format string>, '-', value/10, value%10 );
    >
    > In certain cases I want to print the minus sign character, in other
    > cases I want to skip it.
    > If I want to print the sign, it is very simple:
    > <format string> = "%c%d.%1d"
    > What about if I want to print only the last two parameters of
    > printf(), skipping the sign character?


    Here's one way:

    printf("%s%d.%1d", you_want_a_minus ? "-" : "", value/10, value%10);

    Here's another:

    if(you_want_a_minus)
    {
    putchar('-');
    }
    printf("%d.%1d", value/10, value%10);

    Doubtless there are other ways, too.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 2, 2007
    #2
    1. Advertising

  3. pozz

    Guest

    On 2 May, 15:12, pozz <> wrote:
    > Is there some modifier that skips a parameter of a printf?
    >
    > For example, I pass three parameters:
    > printf( <format string>, '-', value/10, value%10 );
    >
    > In certain cases I want to print the minus sign character, in other
    > cases I want to skip it.
    > If I want to print the sign, it is very simple:
    > <format string> = "%c%d.%1d"
    > What about if I want to print only the last two parameters of
    > printf(), skipping the sign character?


    I rather liked (for some value of the term "like") this solution :)

    #include <stdio.h>
    void printValue(int value) {
    printf("%*4$.*4$s %d %d\n",
    "-",abs(value)/10,abs(value)%10,!(!(value < 0)));
    }
    int main( void )
    {
    printValue(13);
    printValue(-42);
    return 0;
    }
    , May 2, 2007
    #3
  4. pozz wrote:
    > Is there some modifier that skips a parameter of a printf?
    >
    > For example, I pass three parameters:
    > printf( <format string>, '-', value/10, value%10 );
    >
    > In certain cases I want to print the minus sign character, in other
    > cases I want to skip it.
    > If I want to print the sign, it is very simple:
    > <format string> = "%c%d.%1d"
    > What about if I want to print only the last two parameters of
    > printf(), skipping the sign character?


    Here's a crazy thought: use the appropriate specifiers and arguments in
    the first place. Think about it: you must have some test somewhere to
    know when to print the '-' and when not to. Otherwise, your hoped for
    specifier could not possible guess what it's supposed to do. You could
    build the test into the arguments to printf, of course, but the test
    must be somewhere. I suspect that you are misspecifying your problem.
    What do you _really_ want to do?
    Martin Ambuhl, May 2, 2007
    #4
  5. pozz

    Ajinkya Guest

    I guess your code doesnot give the expected o/p the OP ordered for....
    what does "%*4$.*4$s %d %d\n" mean....

    On May 2, 8:49 am, wrote:
    > On 2 May, 15:12, pozz <> wrote:
    >
    > > Is there some modifier that skips a parameter of a printf?

    >
    > > For example, I pass three parameters:
    > > printf( <format string>, '-', value/10, value%10 );

    >
    > > In certain cases I want to print the minus sign character, in other
    > > cases I want to skip it.
    > > If I want to print the sign, it is very simple:
    > > <format string> = "%c%d.%1d"
    > > What about if I want to print only the last two parameters of
    > > printf(), skipping the sign character?

    >
    > I rather liked (for some value of the term "like") this solution :)
    >
    > #include <stdio.h>
    > void printValue(int value) {
    > printf("%*4$.*4$s %d %d\n",
    > "-",abs(value)/10,abs(value)%10,!(!(value < 0)));}
    >
    > int main( void )
    > {
    > printValue(13);
    > printValue(-42);
    > return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -
    Ajinkya, May 3, 2007
    #5
  6. [Please don't top-post in clc.]

    Ajinkya <> wrote:
    > I guess your code doesnot give the expected o/p the OP
    > ordered for....
    > what does [printf(] "%*4$.*4$s %d %d\n" mean....


    It has no meaning in ISO C.

    --
    Peter
    Peter Nilsson, May 3, 2007
    #6
  7. Ajinkya said:

    > I guess your code doesnot give the expected o/p the OP ordered for....
    > what does "%*4$.*4$s %d %d\n" mean....


    In the context of a format specification string for printf, it is
    meaningless.

    <snip>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 3, 2007
    #7
  8. pozz

    Guest

    On 3 May, 05:48, Richard Heathfield <> wrote:
    > Ajinkya said:
    >
    > > I guess your code doesnot give the expected o/p the OP ordered for....
    > > what does "%*4$.*4$s %d %d\n" mean....

    >
    > In the context of a format specification string for printf, it is
    > meaningless.


    In the context of a format specification string for printf() provided
    by a specific library (glibc) it's not meaningless. Mea Culpa for not
    sticking to the bare minimum spec, and simply referring to my man
    pages.

    I think this one may be conformant...

    #include <stdio.h>
    void printValue(int value) {
    printf("%*.*s %d %d\n",(value < 0),(value<0),"-",abs(value)/
    10,abs(value)%10);
    }
    int main( void )
    {
    printValue(13);
    printValue(-42);
    return 0;
    }
    , May 3, 2007
    #8
  9. pozz

    pozz Guest

    ha scritto:

    > void printValue(int value) {
    > printf("%*.*s %d %d\n",(value < 0),(value<0),"-",abs(value)/
    > 10,abs(value)%10);
    > }


    It isn't useful for me.
    I explain better my situations.

    I have an hw architecture where it is impossible to use floating point
    numbers (it's a small microcontroller).

    I have a set of int variables: some variables can be negative and
    positive, other variables can be only positive. I want to display the
    sign (minus or plus) for the variables that could be negative/
    positive, and I don't want to display the sign for the other
    variables.

    The integer value represents a fractional number with only one digit
    after the decimal point (if the variable is 32, the display valued
    should be +3.2 or 3.2, depending if that variable can be negative or
    not).

    Additional, some variables can have a suffix ("s", "ms", "Hz", and so
    on).

    I defined an array of costant formatting string, one for each
    variable, like the following:
    "%?%d.%1dms" - variable without sign and "ms" suffix (? is to skip the
    sign, see below)
    "%c%d.%1d" - variable with sign and no suffix
    and so on

    I created a function like this:

    void printValue( int value, char *fmtstr ) {
    if( value<0 ) {
    value = -value;
    printf( fmtstr, "-", value/10, value%10 );
    } else
    printf( fmtstr, "+", value/10, value%10 );

    This method should work if there is a modifier (the ? character above)
    that skips a parameter of printf().
    pozz, May 3, 2007
    #9
  10. pozz

    Eric Sosman Guest

    pozz wrote On 05/03/07 08:39,:
    > ha scritto:
    >
    >
    >>void printValue(int value) {
    >> printf("%*.*s %d %d\n",(value < 0),(value<0),"-",abs(value)/
    >>10,abs(value)%10);
    >>}

    >
    >
    > It isn't useful for me.
    > I explain better my situations.
    >
    > I have an hw architecture where it is impossible to use floating point
    > numbers (it's a small microcontroller).
    >
    > I have a set of int variables: some variables can be negative and
    > positive, other variables can be only positive. I want to display the
    > sign (minus or plus) for the variables that could be negative/
    > positive, and I don't want to display the sign for the other
    > variables.
    >
    > The integer value represents a fractional number with only one digit
    > after the decimal point (if the variable is 32, the display valued
    > should be +3.2 or 3.2, depending if that variable can be negative or
    > not).
    >
    > Additional, some variables can have a suffix ("s", "ms", "Hz", and so
    > on).
    >
    > I defined an array of costant formatting string, one for each
    > variable, like the following:
    > "%?%d.%1dms" - variable without sign and "ms" suffix (? is to skip the
    > sign, see below)
    > "%c%d.%1d" - variable with sign and no suffix
    > and so on
    >
    > I created a function like this:
    >
    > void printValue( int value, char *fmtstr ) {
    > if( value<0 ) {
    > value = -value;
    > printf( fmtstr, "-", value/10, value%10 );
    > } else
    > printf( fmtstr, "+", value/10, value%10 );
    >
    > This method should work if there is a modifier (the ? character above)
    > that skips a parameter of printf().


    For a value that should show a sign, use a format
    string beginning with "%s". For a value that should
    show no sign, begin with "%.0s".

    Note that `value = -value;' may not work as desired
    for very large negative `value'.

    --
    Eric Sosman, May 3, 2007
    #10
  11. pozz

    Chris Torek Guest

    >pozz wrote On 05/03/07 08:39,:
    >> I explain better my situations.


    Always a good idea. :)

    >> I have an hw architecture where it is impossible to use floating point
    >> numbers (it's a small microcontroller).
    >>
    >> I have a set of int variables: some variables can be negative and
    >> positive, other variables can be only positive. I want to display the
    >> sign (minus or plus) for the variables that could be negative/
    >> positive, and I don't want to display the sign for the other
    >> variables.
    >>
    >> The integer value represents a fractional number with only one digit
    >> after the decimal point (if the variable is 32, the display valued
    >> should be +3.2 or 3.2, depending if that variable can be negative or
    >> not).
    >>
    >> Additional, some variables can have a suffix ("s", "ms", "Hz", and so
    >> on).


    [and a function that prints one variable]

    >> void printValue( int value, char *fmtstr ) {
    >> if( value<0 ) {
    >> value = -value;
    >> printf( fmtstr, "-", value/10, value%10 );
    >> } else
    >> printf( fmtstr, "+", value/10, value%10 );
    >> }


    You could, of course, just have *two* functions: one to print a
    "signed variable" and one to print an "unsigned variable". The
    two functions might be, for instance:

    void printSignedValue(int value, char *fmtstr);
    void printUnsignedValue(unsigned int value, char *fmtstr);

    This also has the advantage of matching the actual type of the
    variable, assuming of course that you use "unsigned int" for those
    variables that are not signed. :)

    In article <1178202202.97834@news1nwk>
    Eric Sosman <> wrote:
    > For a value that should show a sign, use a format
    >string beginning with "%s". For a value that should
    >show no sign, begin with "%.0s".


    This will, of course, also work (with the caveat):

    > Note that `value = -value;' may not work as desired
    >for very large negative `value'.


    (Specifically, when value is INT_MIN.)
    --
    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 3, 2007
    #11
  12. pozz schrieb:
    > ...
    > I have a set of int variables: some variables can be negative and
    > positive, other variables can be only positive. I want to display the
    > sign (minus or plus) for the variables that could be negative/
    > positive, and I don't want to display the sign for the other
    > variables.
    >
    > The integer value represents a fractional number with only one digit
    > after the decimal point (if the variable is 32, the display valued
    > should be +3.2 or 3.2, depending if that variable can be negative or
    > not).
    >
    > Additional, some variables can have a suffix ("s", "ms", "Hz", and so
    > on).
    >
    > I defined an array of costant formatting string, one for each
    > variable, like the following:
    > "%?%d.%1dms" - variable without sign and "ms" suffix (? is to skip the
    > sign, see below)
    > "%c%d.%1d" - variable with sign and no suffix
    > and so on
    >
    > I created a function like this:
    >
    > void printValue( int value, char *fmtstr ) {
    > if( value<0 ) {
    > value = -value;
    > printf( fmtstr, "-", value/10, value%10 );
    > } else
    > printf( fmtstr, "+", value/10, value%10 );
    >
    > This method should work if there is a modifier (the ? character above)
    > that skips a parameter of printf().


    If your C implementation adheres to the current ISO standard or at least
    does truncation toward zero when integers are divided, you could use
    something like the following.

    "%d.%d ms" - variable without sign and "ms" suffix
    "%+d.%d" - variable with sign and no suffix
    and so on

    void printValue(int value, char *fmtstr)
    {
    printf(fmtstr, value/10, abs(value%10));
    }
    --
    DPS
    Dietmar Schindler, May 8, 2007
    #12
  13. pozz

    Eric Sosman Guest

    Dietmar Schindler wrote On 05/08/07 07:13,:
    > pozz schrieb:
    >
    >>...
    >>I have a set of int variables: some variables can be negative and
    >>positive, other variables can be only positive. I want to display the
    >>sign (minus or plus) for the variables that could be negative/
    >>positive, and I don't want to display the sign for the other
    >>variables.
    >>
    >>The integer value represents a fractional number with only one digit
    >>after the decimal point (if the variable is 32, the display valued
    >>should be +3.2 or 3.2, depending if that variable can be negative or
    >>not).
    >>
    >>Additional, some variables can have a suffix ("s", "ms", "Hz", and so
    >>on).
    >>
    >>I defined an array of costant formatting string, one for each
    >>variable, like the following:
    >>"%?%d.%1dms" - variable without sign and "ms" suffix (? is to skip the
    >>sign, see below)
    >>"%c%d.%1d" - variable with sign and no suffix
    >>and so on
    >>
    >>I created a function like this:
    >>
    >>void printValue( int value, char *fmtstr ) {
    >> if( value<0 ) {
    >> value = -value;
    >> printf( fmtstr, "-", value/10, value%10 );
    >> } else
    >> printf( fmtstr, "+", value/10, value%10 );
    >>
    >>This method should work if there is a modifier (the ? character above)
    >>that skips a parameter of printf().

    >
    >
    > If your C implementation adheres to the current ISO standard or at least
    > does truncation toward zero when integers are divided, you could use
    > something like the following.
    >
    > "%d.%d ms" - variable without sign and "ms" suffix
    > "%+d.%d" - variable with sign and no suffix
    > and so on
    >
    > void printValue(int value, char *fmtstr)
    > {
    > printf(fmtstr, value/10, abs(value%10));
    > }


    Misbehaves for -10 < value < 0, where value/10 is
    zero and shows no minus sign (or even shows a plus).

    --
    Eric Sosman, May 8, 2007
    #13
  14. pozz

    Guest

    On 8 Mai, 16:08, Eric Sosman <> wrote:
    > Dietmar Schindler wrote On 05/08/07 07:13,:
    > ...
    > > void printValue(int value, char *fmtstr)
    > > {
    > > printf(fmtstr, value/10, abs(value%10));
    > > }

    >
    > Misbehaves for -10 < value < 0, where value/10 is
    > zero and shows no minus sign (or even shows a plus).


    You are, of course, right.
    , May 8, 2007
    #14
    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. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  3. John Devereux

    skipping parameters with printf

    John Devereux, Sep 8, 2005, in forum: C Programming
    Replies:
    12
    Views:
    816
    Robert Gamble
    Sep 11, 2005
  4. kito
    Replies:
    2
    Views:
    413
  5. azza

    printf affects following printf/s

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

Share This Page