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