473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

skipping parameters with printf

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" the parameter if
it is at the end of the parameter list?
For example,

printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */
Thanks,
--

John Devereux
Nov 15 '05 #1
12 7564
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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" the parameter if
it is at the end of the parameter list? For example, printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


Answering your second question: when printf() and kin reach the
end of the conversion specification, they stop attempting to convert
values, so your second example will be fine.

Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.

There is a common extension to the printf family which offers a
prefix immediately after the %, consisting of an argument number
followed by a $ and when this appears, the specified value is the
next one converted. Note that $ is not a part of the standard C
character set, so this extension is doubly non-portable.
--
Any sufficiently old bug becomes a feature.
Nov 15 '05 #2
John Devereux <jd******@THISdevereux.me.uk> wrote:
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" the parameter if
it is at the end of the parameter list?

For example,

printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


It's ok to pass more arguments to printf than are to be consumed by
the format specifiers (but, of course, not the other way round).

From ISO/IEC 9899:1999 (E) 7.19.6.1p2:
[...] If there are insufficient arguments for the format, the
behavior is undefined. If the format is exhausted while arguments
remain, the excess arguments are evaluated (as always) but are
otherwise ignored. [...]

Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #3
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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" the parameter if
it is at the end of the parameter list?

For example,

printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


Answering your second question: when printf() and kin reach the
end of the conversion specification, they stop attempting to convert
values, so your second example will be fine.

Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.


There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.
There is a common extension to the printf family which offers a
prefix immediately after the %, consisting of an argument number
followed by a $ and when this appears, the specified value is the
next one converted. Note that $ is not a part of the standard C
character set, so this extension is doubly non-portable.


I am pretty sure the $n feature is part of the Posix standard so it is
portable systems implementing Posix. When using this notation you have
to use it exclusively when accessing parameters used in the format
string. You can't use this feature to "skip" arguments either since there
can't be gaps in the indexes for the reason mentioned above.

Robert Gamble
Nov 15 '05 #4
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:


printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


Have you considered strftime()?
Nov 15 '05 #5
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:
There is a common extension to the printf family which offers a
prefix immediately after the %, consisting of an argument number
followed by a $ and when this appears, the specified value is the
next one converted. Note that $ is not a part of the standard C
character set, so this extension is doubly non-portable.

I am pretty sure the $n feature is part of the Posix standard so it is
portable systems implementing Posix.

It is not part of POSIX.1-1990; possibly a different POSIX binding.

When using this notation you have
to use it exclusively when accessing parameters used in the format
string. You can't use this feature to "skip" arguments either since there
can't be gaps in the indexes for the reason mentioned above.


That sounds reasonable; I'd have to find the appropriate standard to
see. The man page I happen to be looking at (SGI IRIX) phrases it this way:

When numbered argument specifications are used, specifying the Nth
argument requires that all the leading arguments, from the first to the
(N-1)th, be specified at least once, in a consistent manner, in the
format string.

and there is no format specifier or modifier present on this system that
allows for the possibility of supressing output.
--
Entropy is the logarithm of probability -- Boltzmann
Nov 15 '05 #6
On Thu, 08 Sep 2005 22:10:04 +0000, Walter Roberson wrote:
In article <pa****************************@gmail.com>, Robert Gamble
<rg*******@gmail.com> wrote:
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:

There is a common extension to the printf family which offers a prefix
immediately after the %, consisting of an argument number followed by
a $ and when this appears, the specified value is the next one
converted. Note that $ is not a part of the standard C character set,
so this extension is doubly non-portable.
I am pretty sure the $n feature is part of the Posix standard so it is
portable systems implementing Posix.

It is not part of POSIX.1-1990; possibly a different POSIX binding.


It's in the 2003 Edition but it is marked as an XSI entension, which I
didn't realize in my initial post as I hadn't checked, so it is possible
that some Posix conforming platforms won't support it.
When using this notation you have
to use it exclusively when accessing parameters used in the format
string. You can't use this feature to "skip" arguments either since
there can't be gaps in the indexes for the reason mentioned above.


That sounds reasonable; I'd have to find the appropriate standard to
see. The man page I happen to be looking at (SGI IRIX) phrases it this
way:

When numbered argument specifications are used, specifying the Nth
argument requires that all the leading arguments, from the first to
the (N-1)th, be specified at least once, in a consistent manner, in
the format string.


Here is how my man page puts it (glibc 2.3.4):

The C99 standard does not include the style using `$', which comes from
the Single Unix Specification. If the style using `$' is used, it must
be used throughout for all conversions taking an argument and all width
and precision arguments, but it may be mixed with `%%' formats which do
not consume an argument. There may be no gaps in the numbers of arguments
specified using `$'; for example, if arguments 1 and 3 are specified,
argument 2 must also be specified somewhere in the format string.

If you want to look it up in the standard its in the "System Interfaces"
volume of the IEEE Std 1003.1 specification (I was looking at the 2003
Edition).

Robert Gamble
Nov 15 '05 #7
an******@example.com (Anonymous 7843) writes:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:


printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


Have you considered strftime()?


I probably should have...

Thanks all for the help.

--

John Devereux
Nov 15 '05 #8
On Thu, 08 Sep 2005 17:47:57 -0400, Robert Gamble wrote:
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:


....
Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.


There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.


It would be easy enough to skip values using a mechanism similar to
scanf()'s * assignment suppression flag. The conversion specifier would
still determine the type of the corresponding argument.

Lawrence
Nov 15 '05 #9
Lawrence Kirby wrote:
On Thu, 08 Sep 2005 17:47:57 -0400, Robert Gamble wrote:
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:


...
Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.


There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.


It would be easy enough to skip values using a mechanism similar to
scanf()'s * assignment suppression flag. The conversion specifier would
still determine the type of the corresponding argument.


Right, the type would be required to do this, that was the whole point.
I was referring to a mechanism to "skip the next n aguments" without
providing any other information about them.

Robert Gamble

Nov 15 '05 #10
"Robert Gamble" <rg*******@gmail.com> writes:
Lawrence Kirby wrote:
On Thu, 08 Sep 2005 17:47:57 -0400, Robert Gamble wrote:
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:


...
> Answering your first question: The C89 standard does not offer
> any mechanism to "skip" values or convert them out of order.

There is no way to do this in C99 either. In fact, I don't even
think it would be possible to design a version that did do this
with the current stdarg facility since it would need to know the
type of the value being skipped to access arguments after it.


It would be easy enough to skip values using a mechanism similar
to scanf()'s * assignment suppression flag. The conversion
specifier would still determine the type of the corresponding
argument.


Right, the type would be required to do this, that was the whole
point. I was referring to a mechanism to "skip the next n aguments"
without providing any other information about them.


It was indeed the scanf suppression flag that made me think it might
be worth asking about a printf equivalent.
--

John Devereux
Nov 15 '05 #11
Groovy hepcat Robert Gamble was jivin' on Thu, 08 Sep 2005 17:47:57
-0400 in comp.lang.c.
Re: skipping parameters with printf's a cool scene! Dig it!
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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" the parameter if
it is at the end of the parameter list?

For example,

printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */


Answering your second question: when printf() and kin reach the
end of the conversion specification, they stop attempting to convert
values, so your second example will be fine.

Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.


There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.


Sure, it could be easily done. You just have to allow a display
suppression flag in the conversion specifier; much like scanf()'s
assignment suppression flag. Then the format string would simply have
a conversion specifier with this flag coresponding to the argument to
be ignored. Simple!
Simple example:

#include <stdio.h>
#include <stdarg.h>

void prints(const char *fmt, ...)
{
va_list arg;
const char *p;
FILE *fp = stdout;

va_start(arg, fmt);

for(p = fmt; *p; p++)
{
if('%' == *p)
{
/* We have a conversion specifier. */
int ignore = 0;
const char *sarg;
int iarg;

p++;
if('!' == *p)
{
/* Flag to ignore coresponding argument. */
ignore = 1;
p++;
}

switch(*p)
{
case 's': /* string */
sarg = va_arg(arg, const char *);
if(!ignore)
{
ignore = 0;
fputs(sarg, fp);
}
break;

case 'd': /* int */
iarg = va_arg(arg, int);
if(!ignore)
{
ignore = 0;
fprintf(fp, "%d", iarg);
}
break;
}
}
else
{
fputc(*p, fp);
}
}

va_end(arg);
}

int main(void)
{
prints("%s %!s %s\n", "foo", "bar", "baz");
prints("%d %d %!d %d\n", 1, 2, 3, 4);
return 0;
}

Here the simplified printf-like function, prints(), understands two
different types of conversion specifiers; one for a string and one for
an int. If the ! flag is used, the coresponding argument will be
ignored.
This program prints the following output:

foo baz
1 2 4

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 15 '05 #12
On Sun, 11 Sep 2005 01:29:07 +0000, Peter "Shaggy" Haywood wrote:
Groovy hepcat Robert Gamble was jivin' on Thu, 08 Sep 2005 17:47:57
-0400 in comp.lang.c.
Re: skipping parameters with printf's a cool scene! Dig it!
On Thu, 08 Sep 2005 17:13:26 +0000, Walter Roberson wrote:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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" the parameter if
it is at the end of the parameter list?

For example,

printf("%02d:%02d:%02d", hours, minutes, seconds); /* 11:59:00 */
printf("%02d:%02d", hours, minutes, seconds); /* 11:59 */

Answering your second question: when printf() and kin reach the
end of the conversion specification, they stop attempting to convert
values, so your second example will be fine.

Answering your first question: The C89 standard does not offer any
mechanism to "skip" values or convert them out of order.


There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.


Sure, it could be easily done. You just have to allow a display
suppression flag in the conversion specifier; much like scanf()'s
assignment suppression flag. Then the format string would simply have
a conversion specifier with this flag coresponding to the argument to
be ignored. Simple!


I meant that this could not be done without providing the type of argument
being skipped. Apparently this was not well-articulated, sorry.

Robert Gamble
Nov 15 '05 #13

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

Similar topics

9
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
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
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? =========
13
by: pozz | last post by:
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...
11
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.