By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,529 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,529 IT Pros & Developers. It's quick & easy.

Fully-portable version of limits.h

P: n/a

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)

--

Frederick Gotham
Sep 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Frederick Gotham <fg*******@SPAM.comwrites:
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.
--
Go not to Usenet for counsel, for they will say both no and yes.
Sep 18 '06 #2

P: n/a
Ben Pfaff posted:
>#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.

Damn, hadn't thought of that...

--

Frederick Gotham
Sep 18 '06 #3

P: n/a
Frederick Gotham wrote:
Ben Pfaff posted:

>>>#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.

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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Sep 19 '06 #4

P: n/a
Frederick Gotham wrote:
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.
Sep 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.