Fully-portable version of limits.h

Discussion in 'C Programming' started by Frederick Gotham, Sep 18, 2006.

  1. 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
    Frederick Gotham, Sep 18, 2006
    #1
    1. Advertising

  2. Frederick Gotham

    Ben Pfaff Guest

    Frederick Gotham <> writes:

    > 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.
    Ben Pfaff, Sep 18, 2006
    #2
    1. Advertising

  3. 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
    Frederick Gotham, Sep 18, 2006
    #3
  4. Frederick Gotham

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Sep 19, 2006
    #4
  5. 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.
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 19, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Wells

    Fully-portable time delay of specified length

    Martin Wells, Oct 8, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,097
    karthikbalaguru
    Oct 11, 2007
  2. Tomás Ó hÉilidhe

    SHA-1 in fully-portable C89?

    Tomás Ó hÉilidhe, Jan 1, 2008, in forum: C Programming
    Replies:
    23
    Views:
    732
    Szabolcs Nagy
    Jan 8, 2008
  3. V Green
    Replies:
    0
    Views:
    829
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    932
    PA Bear [MS MVP]
    Feb 5, 2008
  5. Tomás Ó hÉilidhe

    SHA1 algorithm -- fully portable

    Tomás Ó hÉilidhe, Mar 24, 2008, in forum: C Programming
    Replies:
    4
    Views:
    663
    Bill Reid
    Mar 27, 2008
Loading...

Share This Page