Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #10707 > unrolled thread
| Started by | "V.Subramanian, India" <subramanian100in@yahoo.com> |
|---|---|
| First post | 2011-09-22 07:32 -0700 |
| Last post | 2011-09-22 19:23 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c
portability issues and limits.h "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-09-22 07:32 -0700
Re: portability issues and limits.h James Kuyper <jameskuyper@verizon.net> - 2011-09-22 10:50 -0400
Re: portability issues and limits.h pete <pfiland@mindspring.com> - 2011-09-22 19:23 -0400
| From | "V.Subramanian, India" <subramanian100in@yahoo.com> |
|---|---|
| Date | 2011-09-22 07:32 -0700 |
| Subject | portability issues and limits.h |
| Message-ID | <c8bd942e-a376-4d25-afa7-4908798e9f5d@n35g2000yqf.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-09-22 10:50 -0400 |
| Message-ID | <j5fi02$fj6$1@dont-email.me> |
| In reply to | #10707 |
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
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-09-22 19:23 -0400 |
| Message-ID | <4E7BC366.2DA4@mindspring.com> |
| In reply to | #10707 |
V.Subramanian, India wrote: > '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. > 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. > Please explain. The values on the implementation that you are using, are: "equal or greater in magnitude(absolute value)" just as the standard requires. -- pete
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web