PDA

View Full Version : INT_MIN and INT_MAX vs. LONG_MIN and LONG_MAX



wayfaerer
7th April 2012, 20:14
Since the range of QScrollBar can't exceed INT_MIN or INT_MAX, I looked up what those were equal to. According to cplusplus.com it's -32767 and 32767, but when I use qDebug() to print INT_MIN and INT_MAX, I get the values that cplusplus.com claims are LONG_MIN and LONG_MAX.

I realize this might not be a Qt-specific question, but why is there this discrepancy?

Talei
7th April 2012, 23:03
It's depend on arch, compiler etc.

MingW and MSVC uses 4 bytes (32bits) for int and long (AFAIK for 32 and 64 bit compilation mode).

You can see how it's defined in limits.h.

i.e.: mingw limits.h


/*
* Maximum and minimum values for ints.
*/
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX-1)

#define UINT_MAX 0xffffffff

/*
* Maximum and minimum values for shorts.
*/
#define SHRT_MAX 32767
#define SHRT_MIN (-SHRT_MAX-1)

#define USHRT_MAX 0xffff

/*
* Maximum and minimum values for longs and unsigned longs.
*
* TODO: This is not correct for Alphas, which have 64 bit longs.
*/
#define LONG_MAX 2147483647L
#define LONG_MIN (-LONG_MAX-1)

#define ULONG_MAX 0xffffffffUL

On the cplusplus.com stated minimum ranges for particular data types, let's say guidelines, are from C99 specification. I don't know if C11 change this. In short those are minimum.maximum sizes for all platform's that int should have (i.e. 2 bytes, 16bis or 4 bytes 32 bits).

One can say that all modern platform like Windows, Linux, MaCOSX use int as 32 bits (there are different types for 64bit int). According to this: http://www.viva64.com/en/a/0030/ even for 64bit CPU's. Of course it's not all true, You can see that in limits.h for Alphas.

wayfaerer
7th April 2012, 23:05
Thanks for the info... very interesting!

Talei
7th April 2012, 23:20
Btw. more about standard can be found here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/
and according to :
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
c.3.9.1 page 62. paragraph 2:


There are five signed integer types : “signed char”, “short int”, “int”, and “long int”., and “long long int”.
In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size
suggested by the architecture of the execution environment44); the other signed integer types are provided to meet special
needs.
44* that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

And qt has it's q-dataType- that is the same across all supported platforms. So if You use qint then that int is 32 bit everywhere where qt runs.