Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160370
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: simple positional printf code review |
| Date | 2021-04-26 20:39 +0000 |
| Organization | The Pitcher Digital Freehold |
| Message-ID | <s678d7$8uf$1@dont-email.me> (permalink) |
| References | <6e8017a1-ca34-4e99-b541-b2ba87b9cc66n@googlegroups.com> |
On Mon, 26 Apr 2021 13:29:30 -0700, Thiago Adams wrote:
> I need a function similar of snprint where I can specify
> the index of the argument in any order and reuse the
> same argument.
[snip]
> but I am not sure if we can find the same function one linux
> and them I decided to implement it.
The Linux glibc implementation of the printf() family of functions recognizes
the %?$ modifier (as in "%3?s"), for specification of /which/ argument to use
to satisfy the formatting; "%3?s" would specify that this specific "%s" format
string will use the third argument, rather than the next argument.
So, in place of your
"A %1 B %2 C %1"
format list, you would specify
"A %1?s B %2?s C %1?s"
PRINTF(3) Linux Programmer's Manual PRINTF(3)
NAME
printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf,
vdprintf, vsprintf, vsnprintf - formatted output conversion
...
Format of the format string
...
The arguments must correspond properly (after type promotion) with the
conversion specifier. By default, the arguments are used in the order
given, where each '*' (see Field width and Precision below) and each
conversion specifier asks for the next argument (and it is an error if
insufficiently many arguments are given). One can also specify explic-
itly which argument is taken, at each place where an argument is
required, by writing "%m$" instead of '%' and "*m$" instead of '*',
where the decimal integer m denotes the position in the argument list
of the desired argument, indexed starting from 1. Thus,
printf("%*d", width, num);
and
printf("%2$*1$d", width, num);
are equivalent.
--
Lew Pitcher
"In Skills, We Trust"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 13:29 -0700
Re: simple positional printf code review Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-26 20:39 +0000
Re: simple positional printf code review Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-26 20:49 +0000
Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 14:00 -0700
Re: simple positional printf code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-26 22:09 +0100
Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 14:18 -0700
Re: simple positional printf code review Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-27 01:46 +0100
Re: simple positional printf code review "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-26 18:19 -0700
Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-27 04:44 -0700
Re: simple positional printf code review Geoff <geoff@invalid.invalid> - 2021-04-26 17:58 -0700
Re: simple positional printf code review Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 15:58 +0000
Re: simple positional printf code review Thiago Adams <thiago.adams@gmail.com> - 2021-04-26 13:53 -0700
Re: simple positional printf code review Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-26 21:53 +0000
csiph-web