Question on ints and long ints

Posted by Nick Cash on Sat 10 Jan 2004 07:41 AM — 5 posts, 19,715 views.

USA #0
I was just wondering this for a while and so I decided to post the question here. As I've delt with it before, and how it is on my C programming book, it says int goes up to 32,767 or something close (not sure if thats the exact number off the top of my head). So my question is this, why do the millions/billions vnum snippets for smaug change all sh_int to ints? Is it different in smaug? To get billions of vnums wouldn't they need to be long ints?

Just a question. Thanks in advance.
Australia #1
The standard size of an 'int' defaults to the 'word size' of the architecture, and the word size is usually the same as the register size of the CPU, but not always. So if a system has a 64 bit CPU, and the CPU has 64 bit general purpose registers, then an 'int' will pretty much always be 64 bits, but this is not guaranteed. For example, on the sparc64 platform, the compiler may opt for 32 bit binaries, which means you get 32 bit ints, pointers, etc, even though the general purpose registers are 64 bits, so essentially, the size of an 'int' is horribly unreliable and should never be taken for granted if you want to maintain some sort of portability.
Amended on Sat 10 Jan 2004 08:33 AM by Dave
USA #2
On the other hand, a short int (sh_int) is generally guaranteed to be 16 bytes in size, so that's why it gets rid of short. However, Dave is right in that technically speaking the size of "int" is unreliable, and you should be using a long instead.
Australia Forum Administrator #3
Quite right - in fact to get a guaranteed size the billion number vnum should really use "long" not "int", as Ksilyan said.

Your system should have a file limits.h somewhere, in it are the limits for your particular case, like this:


#define SCHAR_MAX       127             /* min value for a signed char */
#define SCHAR_MIN       (-128)          /* max value for a signed char */

#define UCHAR_MAX       255             /* max value for an unsigned char */
#define CHAR_MAX        127             /* max value for a char */
#define CHAR_MIN        (-128)          /* min value for a char */

#define USHRT_MAX       65535           /* max value for an unsigned short */
#define SHRT_MAX        32767           /* max value for a short */
#define SHRT_MIN        (-32768)        /* min value for a short */

#define UINT_MAX        0xffffffff      /* max value for an unsigned int */
#define INT_MAX         2147483647      /* max value for an int */
#define INT_MIN         (-2147483647-1) /* min value for an int */

#define ULONG_MAX       0xffffffff      /* max value for an unsigned long */
#define LONG_MAX        2147483647      /* max value for a long */
#define LONG_MIN        (-2147483647-1) /* min value for a long */


You can see that in this case an int and a long both have the same size (2147483647) which is effectively slightly over 2 billion.
USA #4
Alright. Thanks for confirming my thoughts. :)