Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #675

Re: PRI* considered harmful

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: PRI* considered harmful
Date 2011-07-09 01:04 -0700
Organization None to speak of
Message-ID <ln62nbyj5g.fsf@nuthaus.mib.org> (permalink)
References <87mxgqhk3x.fsf@once.weird.com>

Show all headers | View raw


"Greg A. Woods" <woods@robohack.org> writes:
[...]
> In order to avoid warnings from some versions of GCC (and perhaps other
> compilers as well -- I haven't had the recent pleasure) on some types of
> systems you will still have to cast some variables declared using
> standard typedef names, such as time_t, to the appropriate int*_t type,
> even when using an appropriate sized PRI* macro with printf(), eg.g. as
> in:
>
> 	typedef time_t long;
>
> 	time_t t = 0;
>
> 	printf("%" PRId32 "\n", (int32_t) t);

Why would you cast a time_t value to int32_t?  Can you show a single
example of someone (other than you) doing so?

[...]
> On other systems PRId32 will be "ld" (sadly even on some 32-bit systems
> with 32-bit int and long!), and if "int32_t" isn't also known as "long"
> down under then you're still screwed.

(I've asked you elsewhere to support this claim.)

[...]

> I've also seen it suggested that _every_ non-base-integer type _always_
> be cast to [u]intmax_t and printed using PRI[ud]MAX:
>
> 	printf("%" PRIuMAX, (uintmax_t) uival);
> 	printf("%" PRIdMAX, (intmax_t) t); /* for signed values */
>
> Now we're safe for every possible underlying type of any typedef name,
> home baked, or standardised.
>
> However if you're always going to do that, then it's far simpler to
> avoid the possibly confusing and unnecessarily verbose (and potentially
> incorrectly defined) use of PRI*MAX and just use "%jd" or "%ju" as
> necessary.  The only advantage of PRI*MAX is that it'll make your code
> more easily ported to non-conforming environments (i.e. those without
> "%jd" et al, and even if PRI*MAX are defined wrong you can still fix it
> with #undef).

I agree that using "%jd" or "%ju" is cleaner than using the PRI* macros.
I certainly don't always think that terseness is a virtue, but in this
case I do prfer the terse version to the more verbose version.

But the argument about non-conforming environments is, frankly, just
silly.  There are pre-C99 implementations that don't support "%jd", and
pre-C99 implementations that don't support the PRI* macros.  I seriously
doubt that there are any real implementations that provide the PRI*MAX
macros but define them incorrectly.  (And again, if you claim that there
are, please provide a specific example.)

> FYI:  The theory here is that the potentially additional heavy lifting
> by printf() is trivial compared to the programmer overhead of trying to
> find and/or define, and/or use, the right PRI* macros.
>
>
> 	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.

Currently, printf formats are specific to the *type* of the argument,
not to its size.  Your proposal would probably work for almost all
existing implementations, but it would require the standard to impose
requirements on implementations that it currently doesn't impose.

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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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