[lug] OT: My own C/C++ mix Q

D. Stimits stimits at idcomm.com
Wed Apr 17 14:12:16 MDT 2002


"Scott A. Herod" wrote:
> 
> "D. Stimits" wrote:
> 
> > Second, on sscanf() and sprintf(), a %lu can be used for an unsigned
> > long format. I *think* %llu is good for the 64 bit unsigned long long,
> > or at least the warnings go away (this is on g++ RH 7.1). Is %llu the
> > correct format string for 64 bit unsigned longs (my references are too
> > old to even know about long long)?
> 
> Yes on this one.  However, I don't know that you can guarantee that
> long long is defined and is 64 bit.  Check in limits.h which for example
> in 2.96 has:
> 
> /* Minimum and maximum values a `signed long long int' can hold.  */
> #   define LLONG_MAX    9223372036854775807LL
> #   define LLONG_MIN    (-LLONG_MAX - 1LL)
> 
> /* Maximum value an `unsigned long long int' can hold.  (Minimum is 0.)
> */
> #   define ULLONG_MAX   18446744073709551615ULL

The original value was specifically defined similar to this, I can
guarantee it is 64 bit. My main concern isn't so much the underlying
data size, but how the format conversion specifications should be
defined. Long ago there wasn't a long long, the old C functions for
string manipulation like sprintf, sscanf, and a lot of others, use for
example "%i" for integer, "%d" for double, and "%l" for long...but there
was no specifier that I know of from back then for long long. I'm hoping
to confirm that a modern sscanf and sprintf are intended to have
unsigned long long as "%llu" (that it isn't a fluke or bug causing the
warning to go away about type conversion), and long long as "%ll" within
those format functions (versus the actual number, I'm only concerned
with formatting it as a char* string for display using C string
functions), as the correct way to display long long and ulong long.

D. Stimits, stimits at idcomm.com



More information about the LUG mailing list