portability issues and limits.h

Discussion in 'C Programming' started by V.Subramanian, India, Sep 22, 2011.

  1. I am using gcc 3.4.3 under RedHat Linux.

    For the discussion sake, the following lines alone are presented from
    the limits.h file in the above implementation.

    /* Minimum and maximum values a `signed int' can hold. */
    # define INT_MIN (-INT_MAX - 1)
    # define INT_MAX 2147483647

    But the ISO C99 Standard(the document is n1256.pdf published in 2007)
    mentions the following in '5.2.4.2.1 Sizes of integer types
    <limits.h>' :

    The implementation-defined values shall be equal or greater in
    magnitude(absolute value) to those shown with the same sign.

    - minimum value for an object of type of int
    INT_MIN -32767

    - maximum value for an object of type of int
    INT_MAX +32767

    If I use the #defined macros INT_MIN and INT_MAX from limits.h, then
    under the implementation that I use, the corresponding values are
    -2147483648 and +2147483647 respectively whereas the ISO C99 Standard
    requires lower absolute values than these. So if my program contains
    expressions which assume the larger range of this particular
    implementation-defined values of INT_MIN and INT_MAX as -2147483648
    and +2147483647, then my program will not be portable because under a
    different implementation, the absolute values may be lower. In order
    to make my program portable, can I declare two variables in my program
    as follows:

    const int int_min_val = -32767;
    const int int_max_val = 32767;

    and use upto these limits only(If higher ranges are required by the
    program, then I will have to decalare similar variables corresponding
    to long, long long types with values as per the ISO C99 Standard) ?
    Will it work ?

    What is the practice adopted in real-world applications ?

    Please explain.

    Thanks
    V.Subramanian
     
    V.Subramanian, India, Sep 22, 2011
    #1
    1. Advertising

  2. V.Subramanian, India

    James Kuyper Guest

    On 09/22/2011 10:32 AM, V.Subramanian, India wrote:
    > I am using gcc 3.4.3 under RedHat Linux.
    >
    > For the discussion sake, the following lines alone are presented from
    > the limits.h file in the above implementation.
    >
    > /* Minimum and maximum values a `signed int' can hold. */
    > # define INT_MIN (-INT_MAX - 1)
    > # define INT_MAX 2147483647
    >
    > But the ISO C99 Standard(the document is n1256.pdf published in 2007)
    > mentions the following in '5.2.4.2.1 Sizes of integer types
    > <limits.h>' :
    >
    > The implementation-defined values shall be equal or greater in
    > magnitude(absolute value) to those shown with the same sign.
    >
    > - minimum value for an object of type of int
    > INT_MIN -32767
    >
    > - maximum value for an object of type of int
    > INT_MAX +32767
    >
    > If I use the #defined macros INT_MIN and INT_MAX from limits.h, then
    > under the implementation that I use, the corresponding values are
    > -2147483648 and +2147483647 respectively whereas the ISO C99 Standard
    > requires lower absolute values than these. So if my program contains
    > expressions which assume the larger range of this particular
    > implementation-defined values of INT_MIN and INT_MAX as -2147483648
    > and +2147483647, then my program will not be portable because under a
    > different implementation, the absolute values may be lower. In order
    > to make my program portable, can I declare two variables in my program
    > as follows:
    >
    > const int int_min_val = -32767;
    > const int int_max_val = 32767;
    >
    > and use upto these limits only(If higher ranges are required by the
    > program, then I will have to decalare similar variables corresponding
    > to long, long long types with values as per the ISO C99 Standard) ?
    > Will it work ?


    Yes. It's excessively clumsy and usually unnecessary, but it will work.
    You're better off using INT_MIN and INT_MAX, however.

    > What is the practice adopted in real-world applications ?


    A better approach is to choose the type based upon the desired range,
    rather than choosing a fixed type and using only it's minimum range.
    Example: if you need to be able to store any number from 0 to 100000:

    #if INT_MAX > 1000000
    typedef int my_type;
    #else
    typedef long my_type;
    #endif

    However, for most purposes, it would be simpler to chose one of the
    int_fastN_t types from <stdint.h>.
    --
    James Kuyper
     
    James Kuyper, Sep 22, 2011
    #2
    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. MJL

    portability issues

    MJL, Aug 1, 2004, in forum: C++
    Replies:
    11
    Views:
    836
  2. OzBob

    Portability / compatibility issues

    OzBob, Jan 15, 2006, in forum: C Programming
    Replies:
    23
    Views:
    866
    Keith Thompson
    Jan 16, 2006
  3. Replies:
    17
    Views:
    840
    Kevin Handy
    Apr 26, 2006
  4. Jonathan Lamothe

    fread/fwrite Portability Issues

    Jonathan Lamothe, Jul 22, 2006, in forum: C Programming
    Replies:
    20
    Views:
    1,228
    Dave Thompson
    Jul 31, 2006
  5. Bart Lateur
    Replies:
    5
    Views:
    92
    Dr J R Stockton
    Jan 12, 2007
Loading...

Share This Page