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


Groups > comp.std.c > #633

Re: PRI* considered harmful

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe15.iad.POSTED!00000000!not-for-mail
Newsgroups comp.std.c
Subject Re: PRI* considered harmful
User-Agent Gnus/5.11 (Gnus v5.11) Emacs/22.3 (berkeley-unix)
References <87mxgqhk3x.fsf@once.weird.com> <iv427t$ed2$1@dont-email.me> <8762ndhnfs.fsf@once.weird.com> <lntyaxztes.fsf@nuthaus.mib.org> <87oc15g10i.fsf@once.weird.com> <lnhb6xzmbf.fsf@nuthaus.mib.org>
From "Greg A. Woods" <woods@robohack.org>
X-Face .}E4CW({2'v<<BDc642q8+(>\JefgY&(9#RJD?~_7.KTC$+RklqwlB/<)^)~X0i*<gQhb;P ,]kHmiY*(6L>J?m)SXpHA8'9FHn5Fz*eVTH5=_C>]XK#"dLOyD`#tbEh1_8v-hwwR{.X)+b-!\4mM] 1HqG#"@VSI$1MNvRk\4>qsCclW|Ns4V/2poz=gS-+~m8\%5eVq]a\3.>T>U/f8A
Message-ID <87zkkpef5z.fsf@once.weird.com> (permalink)
Cancel-Lock sha1:i3d4l/4IuddcxhsI3VaGJSyOBJQ=
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Lines 103
NNTP-Posting-Host 24.67.98.78
X-Complaints-To internet.abuse@sjrb.ca
X-Trace newsfe15.iad 1310088377 24.67.98.78 (Fri, 08 Jul 2011 01:26:17 UTC)
NNTP-Posting-Date Fri, 08 Jul 2011 01:26:17 UTC
Date Thu, 07 Jul 2011 18:26:16 -0700
Xref x330-a1.tempe.blueboxinc.net comp.std.c:633

Show key headers only | View raw


Keith Thompson <kst-u@mib.org> writes:
> 
> Presumably an implementation that warns about mismatched printf
> formats, even those that happen to work (say, because int and long
> are the same size) would define the PRI* macros so they avoid those
> warnings when used correctly.

"when used correctly"?  Just how is one to do that?  Without always
using a cast, that is?

BTW, if one does always use a cast, then one can always use the exact
right cast for "%d" or "%ld" or "%u" or "%lu", etc., and thus the PRI*
macros once again prove useless and futile.

> I suggest that, at least in this specific case, you're looking for
> problems that are unlikely to exist in any real implementation.

You mean like in some versions GNU/Linux, or more specifically, I think,
some(all?) versions of glibc where PRId32 is "%ld" even on 32-bit
systems with 32-bit ints and where int32_t is a "signed int"?  At least
as far as I can tell. (I don't generally use GNU/Linux, and I despise
glibc for many other reasons than this :-))


> If you're printing ints and longs, you don't need to use the PRI*
> macros.  Just use "%d" and "%ld", respectively.  Or did I misunderstand
> your point?

That is sort of my point.  The tricky part comes when one wants to print
something like time_t (e.g. knowing that it is an integer in 99.999% of
host environments where one's code can ever be used), then one still has
to decide whether to use "%ld" and cast to a "long", or to use "%lld"
and cast to "long long", or to just give up on performance and always
use "%jd" and cast to "intmax_t".

PRI* are in fact being misused in real-world code, and to the best of my
understanding most people cannot make valid use of them -- i.e. they are
harmful to the goal of making code more portable.  It's hard enough
getting junoir programmers to use casts properly in C, and they still
trip up some of us after dozens of years of using them.

Thus my suggestion of enhancing printf() instead of trying to suggest
that the standard provide more PRI* like macros.  Let's get rid of the
problem entirely, or at least provide a truely safe and innovative
solution for those who would prefer a better way.

Once upon a time C expected the programmer to conscientiously use the
proper types for every function call without anything more than separate
tools like "lint" to sometimes help check for errors.  Now the standard
now offers prototypes and compiler writers have made it possible for
compiles to fail if the programmer makes a mistake with writing
prototypes.  We can do somethin similar for printf() (and scanf()) by
simply making sure that the programmer can always specify exactly what's
being passed as an integer (pointer) value, and we can do it without
changing the fundamental language in any incompatible way.


>> Also, I repeat, C99 strftime() is unable to print the value of a time_t
>> as an integer representation of seconds since the epoch of time_t.  (C99
>> does not have the '%s' specifier that many implementations do have.)
>
> That's because the language doesn't say that time_t represents an
> integer number of seconds since some epoch.  It says only that it's
> an arithmetic type "capable of representing times".  Among many
> other possibilities, it could use the value 20110707233829, or
> 0x20110707233829, or 020110707233829, to represent the current time.
> Or the resolution could be larger or smaller than one second.
> The relationship between time_t values and real times needn't even
> be linear.

I think you missed my point, _entirely_.  C99 strftime() cannot EVER do
what I want, no matter how time_t is defined.  I.e. strftime() IS NOT
the answer -- not even close, not even for just time_t.  Get off your
high horse about time_t!  :-)   It's an example, not the whole problem.


> No, the PRI* macros do not solve the problem of printing raw time_t
> values; they're not intended to.  I do sometimes print such values
> myself; I generally just write something like
>
>     printf("%ld\n", (long)time(NULL));
>
> which isn't entirely portable, but it's good enough for most purposes.

Every bit of code like that will break on POSIX systems in 2038.  :-)
Even on the systems which update time_t to be more than 32 bits.


>>> The only library-defined *integer* type that isn't covered, as far
>>> as I can tell, is wchar_t.  You wouldn't normally want to print that
>>> as integer value anyway.
>>
>> It's not the only one (not even close).
>
> So what are the others?

UTSL!

-- 
						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


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