Topic: printing size_t etc. (was: NULL pointer)
Author: fred@genesis.demon.co.uk (Lawrence Kirby)
Date: 1997/01/15 Raw View
In article <5bauu7$b55@mulga.cs.mu.OZ.AU>
fjh@mundook.cs.mu.OZ.AU "Fergus Henderson" writes:
>"David R. Tribble" <david.tribble@central.beasys.com> writes:
>
>>(BTW, this brings up a few questions about POSIX data types.
>>Specifically, how big are uid_t, gid_t, pid_t, off_t, et al? If I have
>>to print them, do I use "%d" or "%ld"? This is not a problem on 32-bit
>>systems, but it can be on 64-bit CPUs. Personally, I always cast them to
>>long and use "%ld".)
>
>That used to be the correct way to do it in C -- but changes to the C
>standard, specifically the introduction of a "long long" data type --
>mean that this long-standing idiom may break.
Yes, it may but only when the values being printed exceed the range of
a long. I suspect that it won't break the vast majority of code. I'm
not suggesting that it isn't a problem however.
> Instead, the best way to
>do it in C is probably to cast to cast to type `longest', defined by
>
> #ifdef HAVE_LONG_LONG
> typedef long long longest;
> #else
> typedef long longest;
> #endif
>
>where HAVE_LONG_LONG must be determined using autoconf or similar
>techniques.
The code also has to select a suitable printf conversion specifier. That's
possible bit makes printf code messy.
>Of course the way to do it in C++ is much simpler: just use
>`cout << x;'.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/01/12 Raw View
"David R. Tribble" <david.tribble@central.beasys.com> writes:
>(BTW, this brings up a few questions about POSIX data types.
>Specifically, how big are uid_t, gid_t, pid_t, off_t, et al? If I have
>to print them, do I use "%d" or "%ld"? This is not a problem on 32-bit
>systems, but it can be on 64-bit CPUs. Personally, I always cast them to
>long and use "%ld".)
That used to be the correct way to do it in C -- but changes to the C
standard, specifically the introduction of a "long long" data type --
mean that this long-standing idiom may break. Instead, the best way to
do it in C is probably to cast to cast to type `longest', defined by
#ifdef HAVE_LONG_LONG
typedef long long longest;
#else
typedef long longest;
#endif
where HAVE_LONG_LONG must be determined using autoconf or similar
techniques.
Of course the way to do it in C++ is much simpler: just use
`cout << x;'.
(This article is crossposted, so please be careful with follow-ups.)
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]