Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > portability issues and limits.h

Reply
Thread Tools

portability issues and limits.h

 
 
V.Subramanian, India
Guest
Posts: n/a
 
      09-22-2011
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
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-22-2011
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
[u]int_fastN_t types from <stdint.h>.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Treating a string as an array, and portability issues Bart Lateur Javascript 5 01-12-2007 08:50 PM
fread/fwrite Portability Issues Jonathan Lamothe C Programming 20 07-31-2006 05:11 AM
portability issues with ' flag in printf billposer@alum.mit.edu C Programming 17 04-26-2006 03:41 PM
Portability / compatibility issues OzBob C Programming 23 01-16-2006 09:42 PM
portability issues MJL C++ 11 08-04-2004 02:07 AM



Advertisments