🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3876

Re: Why "long long" doesn't work.

#3876
From: Thad Smith
Date: Sun, 27 Aug 1995 09:20
44 lines
2138 bytes
In article <41o4n4$m0l@newsbf02.news.aol.com>,
ffarance@aol.com (FFarance) wrote:

 >> At present long is known to be the largest signed type, so I can
 >> safely do the following:
 >> 
 >> ptrdiff_t x = 0;
 >> printf("%ld", (long) x);
 >> 
 >> (where ptrdiff_t can be replaced by any typedef which I know to be
 >signed & 
 >> integral but don't know the exact type of). This is a very useful
idiom,
 >> and this cannot be done at all if there is no largest type.
 >
 > Unfortunately, this idiom isn't portable.  There is no guarantee
 > that "ptrdiff_t" will fit in a "long", e.g., systems with 48-bit
 > pointers and 32-bit "long" (some memory models in OS/2).  Another
 > example is "fpos_t": it may be larger than "long".

In Standard C ptrdiff_t must be a signed integral type defined in
Standard C (see enumeration of signed integer types in ANSI 3.1.2.5).
There is no integral type in C larger than long int, hence casting to
long should be portable.  Note, however, that overflow is possible as
a result of subtracting pointers.  fpos_t can certainly be larger than
a long -- it isn't even restricted to be a scalar type, let alone
arithmetic or integral, but might be a structure.

 > In fact, if you needed a value to hold a temporary of multiplying
 > an "fpos_t" by a "ptrdiff_t" (assuming this made sense), you could
 > do:
 >        #define FPOS_BIT (EIR_BIT(precof(fpos_t)))
 >        #define PTRDIFF_BIT (EIR_BIT(precof(ptrdiff_t)))
 >        int atleast:(FPOS_BIT+PTRDIFF_BIT) t;
 >
 >        t = y;
 >        t *= z;
 >        printf("result %?d\n",t);
                               ^ precof(t),

Your example makes sense, but would not be portable because fpos_t is
not guaranteed integral.

Thad
Technical Details
Message-ID: <+HCQwQ9ythna084yn@csn.net>
Path: arc1-ca.pugleaf.net!teal.csn.net!not-for-mail
References: <ncfPwAmIBh107h@rcp.co.uk> <41o4n4$m0l@newsbf02.news.aol.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!teal.csn.net!not-for-mail
From: thads@csn.net (Thad Smith)
Newsgroups: comp.std.c
Subject: Re: Why "long long" doesn't work.
Date: 27 Aug 1995 03:20:27 -0600
Organization: T3 Systems
Message-ID: <+HCQwQ9ythna084yn@csn.net>
References: <ncfPwAmIBh107h@rcp.co.uk> <41o4n4$m0l@newsbf02.news.aol.com>
Reply-To: ThadSmith@acm.org
NNTP-Posting-Host: 199.117.27.22