Fully-portable version of limits.h

F

Frederick Gotham

I was pondering over writing a fully-portable version of <limits.h> (e.g.
such things as:

#define UINT_MAX ((unsigned)-1)

, when something occurred to me. Just recently on this newsgroup, I and a few
others posted code which yielded a compile-time constant specifying the
amount of bits which are set in an integer expression; then I thought about
IMAX_BITS, and wondered if this is how IMAX_BITS works? Does it just count
how many bits are set?

Is it possible in C to a write a fully-portable version of <limits.h>? I'll
start off with the easy ones:

#define CHAR_BIT (IMAX_BITS((char unsigned)-1))
#define UCHAR_MAX ((char unsigned)-1 + 0)
#define USHRT_MAX ((short unsigned)-1 + 0)
#define UINT_MAX ((unsigned)-1)
#define ULONG_MAX ((long unsigned)-1)
 
B

Ben Pfaff

Frederick Gotham said:
Is it possible in C to a write a fully-portable version of <limits.h>? I'll
start off with the easy ones:

#define CHAR_BIT (IMAX_BITS((char unsigned)-1))

That's not conforming. CHAR_BIT must be suitable for use in an
#if directive, but constant expressions in #if directives may not
contain casts.
 
F

Frederick Gotham

Ben Pfaff posted:
That's not conforming. CHAR_BIT must be suitable for use in an
#if directive, but constant expressions in #if directives may not
contain casts.


Damn, hadn't thought of that...
 
E

Eric Sosman

Frederick said:
Ben Pfaff posted:





Damn, hadn't thought of that...

And as if that weren't enough, try writing a fully
portable replacement for INT_MIN. Extra credit: CHAR_MIN.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Frederick said:
I was pondering over writing a fully-portable version of <limits.h> (e.g.
such things as:

There is no need to. It comes with the implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top