portability issues and limits.h

  • Thread starter V.Subramanian, India
  • Start date
V

V.Subramanian, India

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
 
J

James Kuyper

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

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top