Skipping parameters in a printf()

P

pozz

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?
 
R

Richard Heathfield

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.
 
M

mark_bluemel

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;
}
 
M

Martin Ambuhl

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 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?
 
A

Ajinkya

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

Peter Nilsson

[Please don't top-post in clc.]

Ajinkya said:
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.
 
R

Richard Heathfield

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>
 
M

mark_bluemel

Ajinkya said:


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;
}
 
P

pozz

(e-mail address removed) 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().
 
E

Eric Sosman

pozz wrote On 05/03/07 08:39,:
(e-mail address removed) ha scritto:




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'.
 
C

Chris Torek

pozz wrote On 05/03/07 08:39,:
Always a good idea. :)

[and a function that prints one variable]

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. :)

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.)
 
D

Dietmar Schindler

pozz said:
...
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));
}
 
E

Eric Sosman

Dietmar Schindler wrote On 05/08/07 07:13,:
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).
 
G

Guest

Dietmar Schindler wrote On 05/08/07 07:13,:
...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top