Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Newsgroups | comp.std.c |
|---|---|
| Subject | Re: PRI* considered harmful |
| References | <87mxgqhk3x.fsf@once.weird.com> <ln62nbyj5g.fsf@nuthaus.mib.org> |
| From | "Greg A. Woods" <woods@robohack.org> |
| Message-ID | <874o2vc7ab.fsf@once.weird.com> (permalink) |
| Date | 2011-07-09 17:23 -0700 |
Keith Thompson <kst-u@mib.org> writes:
>
> "Greg A. Woods" <woods@robohack.org> writes:
> >
> > A Better Improvement(?):
> >
> > Too bad printf(3) et al don't have format flag akin to '*' which allows
> > you to pass the sizeof() and the signedness of an integer type to it so
> > that you don't have to hard-code the integer size in the format
> > string....
> >
> > int128_t foo = -1;
> > uint64_t ubar = 1;
> >
> > #define issigned(v) (((typeof(v)) -1 < 0) ? true : false)
> > #define INTPARAM(v) (int) issigned(v), (int) sizeof(v), v
> >
> > printf("%Si %Si\n", INTPARAM(foo), INTPARAM(ubar));
> >
> > (I chose "%i" to suggest somewhat that it's different from "%d" or "%u",
> > i.e. in that it takes the signedness from the 'S' flag (since I don't
> > see %i used very often in the code I read -- it's definitely the new kid
> > on the block, and perhaps it can grow up more with a new meaning), but
> > of course it need not be -- "%d" and "%u" can also take the sign flag
> > using the 'S' flag, but that may confuse the novices more often than not.)
> >
> > (also, issigned should become a new keyword, as should typeof from GCC,
> > though with the former, the latter isn't needed here :-))
> >
> >
> > Does anyone else think this would be worthwhile? Better ideas anyone?
>
> Let's assume that, for a given implementation, sizeof (int) and sizeof
> (long) are both 4. Your proposal assumes that both these calls:
>
> int i;
> long l;
>
> printf("%Si\n", 1, 4, i);
> printf("%Si\n", 1, 4, l);
>
> must work correctly. The standard doesn't currently guarantee
> that int and long are passed the same way as arguments, even if
> they happen to be the same size.
>
> I think there have been compilers where int and long are the same
> size, but int has more padding bits and a narrower range.
Does that matter in practice? Do we have to assume the padding bits are
zeros for it to work? Finding the sign bit is of course the trick, but
are you sure sizeof() reports the padding as well? I'm not.
In an LP64I32 environment I would expect the CPP result to be as follows
even if the implementation always stores 64-bit words on the stack:
printf("%Si\n", 1, 8, i);
printf("%Si\n", 1, 4, l);
In that case the implementation of printf() would of course already have
to be aware of this issue and it would be trivial for "%Si" to deal with
it.
> I suspect that the effort of making the necessary changes to the
> language standard would exceed the benefit of your proposal --
> especially given that converting to intmax_t and using "%jd", or
> converting to uintmax_t and using "%ju", solves the problem with
> no language or library changes.
Indeed I'm not convinced either that my proposal is much more elegant
than just always using a cast to [u]intmax_t.
Except for the sign handling -- I hate having to find and fix every use
of a variable as a varargs parameter after I change it's signedness, and
I can't count the number of times I've found places where others have
failed to be so careful, or indeed where I have failed to be so careful
in the past.
In fact initially when I was thinking this up I was only thinking of the
sign issue, then I ran across yet another question on a mailing list
about what to do to handle the time_t problem yet again. (You see,
printing time_t raw values is often VERY common in some circles, indeed
important for data and protocol purposes, not just debugging!) (I've
also been wanting to add "%m" (ala syslog(3) to my libc for at least a
decade now too.)
I would probably want to have INTPARAM() actually be named something
more like i_() to make it less abrasive and intrusive. :-)
--
Greg A. Woods
RoboHack
<woods@robohack.ca> living on the edge http://www.robohack.ca/
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-06 19:58 -0700
Re: PRI* considered harmful William Ahern <william@wilbur.25thandClement.com> - 2011-07-06 20:38 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-06 22:26 -0700
Re: PRI* considered harmful Jens Gustedt <Jens.Gustedt@loria.fr> - 2011-07-07 08:52 +0200
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-07 14:11 -0700
Re: PRI* considered harmful Jens Gustedt <Jens.Gustedt@loria.fr> - 2011-07-08 11:58 +0200
Re: PRI* considered harmful lawrence.jones@siemens.com - 2011-07-07 17:58 -0400
Re: PRI* considered harmful Jens Gustedt <Jens.Gustedt@loria.fr> - 2011-07-08 12:02 +0200
Re: PRI* considered harmful lawrence.jones@siemens.com - 2011-07-07 17:57 -0400
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-07 15:42 -0700
Re: PRI* considered harmful lawrence.jones@siemens.com - 2011-07-08 08:56 -0400
Re: PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-07 06:36 -0400
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-07 12:59 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-07 14:12 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-07 15:49 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-07 16:46 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-07 18:26 -0700
Re: PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-07 22:35 -0400
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-07 20:58 -0700
Re: PRI* considered harmful Wojtek Lerch <wojtek_l@yahoo.ca> - 2011-07-08 00:28 -0400
Re: PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-08 07:41 -0400
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-08 23:05 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 00:50 -0700
Re: [incorrectly implementd] PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-09 06:38 -0400
Re: PRI* considered harmful Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2011-07-09 21:07 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-08 09:52 -0700
Re: PRI* considered harmful Richard Kettlewell <rjk@greenend.org.uk> - 2011-07-08 11:45 +0100
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-08 23:14 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 00:47 -0700
Re: PRI* considered harmful Richard Kettlewell <rjk@greenend.org.uk> - 2011-07-09 10:51 +0100
Re: PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-09 06:43 -0400
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-08 09:49 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-08 23:24 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 00:45 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-09 16:18 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 18:57 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-13 09:37 -0700
Re: PRI* considered harmful James Kuyper <jameskuyper@verizon.net> - 2011-07-09 06:46 -0400
Re: PRI* considered harmful Harald van Dijk <truedfx@gmail.com> - 2011-07-09 08:09 -0700
Re: PRI* considered harmful Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-11 16:00 +0300
Re: PRI* considered harmful Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2011-07-09 20:59 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 12:22 -0700
Re: PRI* considered harmful Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2011-07-09 21:32 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 12:54 -0700
Re: PRI* considered harmful Marcin Grzegorczyk <mgrzegor@poczta.onet.pl> - 2011-07-09 23:39 +0200
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-09 16:56 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-09 16:52 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 18:53 -0700
Re: PRI* considered harmful Jens Gustedt <Jens.Gustedt@loria.fr> - 2011-07-10 14:12 +0200
Re: PRI* considered harmful Marcin Grzegorczyk <mgrzegor@poczta.onet.pl> - 2011-07-11 20:22 +0200
Re: PRI* considered harmful Jens Gustedt <Jens.Gustedt@loria.fr> - 2011-07-10 13:57 +0200
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-09 16:49 -0700
Re: PRI* considered harmful Jens Schweikhardt <usenet@schweikhardt.net> - 2011-07-19 19:28 +0000
Re: PRI* considered harmful Tim Rentsch <txr@alumni.caltech.edu> - 2011-07-07 14:13 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 01:04 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-09 17:23 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-09 19:03 -0700
Re: PRI* considered harmful Marcin Grzegorczyk <mgrzegor@poczta.onet.pl> - 2011-07-11 20:40 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-11 11:56 -0700
Re: PRI* considered harmful Marcin Grzegorczyk <mgrzegor@poczta.onet.pl> - 2011-07-11 21:40 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-11 13:16 -0700
Re: PRI* considered harmful Marcin Grzegorczyk <mgrzegor@poczta.onet.pl> - 2011-07-11 23:29 +0200
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-11 14:46 -0700
Re: PRI* considered harmful "Greg A. Woods" <woods@robohack.org> - 2011-07-12 22:18 -0700
Re: PRI* considered harmful Keith Thompson <kst-u@mib.org> - 2011-07-13 09:00 -0700
Re: PRI* considered harmful Richard Kettlewell <rjk@greenend.org.uk> - 2011-07-09 20:30 +0100
csiph-web