Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: PRI* considered harmful |
| Date | 2011-07-08 09:49 -0700 |
| Organization | None to speak of |
| Message-ID | <ln8vs8zpj6.fsf@nuthaus.mib.org> (permalink) |
| References | (2 earlier) <8762ndhnfs.fsf@once.weird.com> <lntyaxztes.fsf@nuthaus.mib.org> <87oc15g10i.fsf@once.weird.com> <lnhb6xzmbf.fsf@nuthaus.mib.org> <87zkkpef5z.fsf@once.weird.com> |
"Greg A. Woods" <woods@robohack.org> writes:
> 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?
If you want to print a value of a type defined in <stdint.h>,
use the macro that corresponds to that type.
If you want to print a value of some other integer type, you'll
need to use some other solution. The PRI* macros will probably
not be part of that solution. Yes, a cast might be necessary;
that's not such a bad thing.
You seem to be criticizing the PRI* macros because they're not
a universal solution to the problem of printing integer values.
They were never intended to be. They're intended only for the
types defined in <stdint.h>.
> 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.
So don't use them.
>> 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 :-))
On my system (Ubuntu 11.04, gcc 4.5.2, glibc 2.13), int32_t is a
typedef for int, and PRId32 is defined as "d".
Can you cite a specific real-world system where the definitions of
int32_t and PRId32 are inconsistent? Can you demonstrate that this
program:
#include <stdio.h>
#include <inttypes.h>
int main(void)
{
int32_t x = 42;
printf("x = %" PRId32 "\n", x);
return 0;
}
triggers a warning about the format string? If it's a big a problem
as you say it is, that shouldn't be difficult.
>> 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".
The PRI* macros are not intended to solve the problem of printing
time_t values. They're intended for printing values of the types
defined in <stdint.h>.
You seem to have a firm grasp on the options for printing raw
time_t values. (Though I suggest that converting to intmax_t isn't
likely to be a significant performance issue. Your program will
probably spend a lot more time just sending the characters to their
destination than it spends formatting them.)
> 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.
I don't doubt that some programmers sometimes misuse the PRI* macros.
There probably isn't a feature of any language that isn't misused
sometimes. Can you provide specific examples of this misuse? And if
so, can you demonstrate that this misuse is the fault of the feature
itself, and not of the programmers who misuse it?
> 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.
You know what? I might actually agree with whatever point you're
trying to make, if I can figure out just what it is. I've never
found any use for the PRI* macros myself. But your underlying point
is obscured by your claims about problems with the PRI* macros that,
as far as I can tell, do not actually exist.
[...]
>>> 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.
I didn't say that strftime() is the answer to *your* problem. It is the
answer to the problem of printing meaningful representations of time_t
values *unless* you need, for some reason, to print the raw value.
>
>> 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.
It won't break if long is 64 bits.
But I write code like the above when the expected useful lifetime
of the program is just a few minutes. If I needed to print a raw
time_t value in production code, I'd use a more reliable method
(like the one I posted earlier in this thread).
>>>> 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!
"Use the source, Luke!", right?
No. I already checked the standard, and didn't find any examples
other than wchar_t. It's possible that I missed something, but
since you're the one making the claim, you can support it.
--
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
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