473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Skipping parameters in a printf()

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?

May 2 '07 #1
13 2392
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.
May 2 '07 #2
On 2 May, 15:12, pozz <pozzu...@libero.itwrote:
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 '07 #3
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?
May 2 '07 #4
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, mark_blue...@pobox.com wrote:
On 2 May, 15:12, pozz <pozzu...@libero.itwrote:
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 -

May 3 '07 #5
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.
May 3 '07 #6
[Please don't top-post in clc.]

Ajinkya <kaleajin...@gmail.comwrote:
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

May 3 '07 #7
On 3 May, 05:48, Richard Heathfield <r...@see.sig.invalidwrote:
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 '07 #8
ma**********@pobox.com 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().

May 3 '07 #9
pozz wrote On 05/03/07 08:39,:
ma**********@pobox.com 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'.

--
Er*********@sun.com
May 3 '07 #10
>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 <Er*********@Sun.COMwrote:
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.
May 3 '07 #11
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
May 8 '07 #12
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).

--
Er*********@sun.com
May 8 '07 #13
On 8 Mai, 16:08, Eric Sosman <Eric.Sos...@sun.comwrote:
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 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2417
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
5
2570
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then...
7
1776
by: Québec | last post by:
How comes the for loop just printf 3 characters? 1 e 7 e 10 e The string mixed by C is : J?an Pi?rr? =========
12
7576
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
11
11038
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1....
2
1912
by: NickPomp | last post by:
Hi, I have to write a slide puzzle program for class. I have the program finished and working except that I can not get the blank space to print out. I wrote code that would find the number I used...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.