# Short/long/signed/unsigned int

• 09-13-2005
scuzzo84
Short/long/signed/unsigned int
Im on a chapeter in my C book talking about Integer Types. It talks a bit about Short/long/signed/unsigned. For example says the long covers a certain range of numbers and same goes for short. But how am I suppose to know what this range is? It says its different for differnt computers.

say I had an int for example

int dogs = 10000000;

is this supposed to be just int, or long?

long int dogs = 10000000; :confused:

A friend also told me not to worry about this since computers now have enough ram, but I still want to understand this.
• 09-13-2005
Thantos
First rule of thumb: Never listen to friends :)

Heres a snippet of mine
Code:

```/* Number of bits in a `char'.  */ #  define CHAR_BIT      8 /* Minimum and maximum values a `signed char' can hold.  */ #  define SCHAR_MIN    (-128) #  define SCHAR_MAX    127 /* Maximum value an `unsigned char' can hold.  (Minimum is 0.)  */ #  define UCHAR_MAX    255 /* Minimum and maximum values a `char' can hold.  */ #  ifdef __CHAR_UNSIGNED__ #  define CHAR_MIN    0 #  define CHAR_MAX    UCHAR_MAX #  else #  define CHAR_MIN    SCHAR_MIN #  define CHAR_MAX    SCHAR_MAX #  endif /* Minimum and maximum values a `signed short int' can hold.  */ #  define SHRT_MIN      (-32768) #  define SHRT_MAX      32767 /* Maximum value an `unsigned short int' can hold.  (Minimum is 0.)  */ #  define USHRT_MAX    65535 /* Minimum and maximum values a `signed int' can hold.  */ #  define INT_MIN      (-INT_MAX - 1) #  define INT_MAX      2147483647 /* Maximum value an `unsigned int' can hold.  (Minimum is 0.)  */ #  define UINT_MAX      4294967295U /* Minimum and maximum values a `signed long int' can hold.  */ #  if __WORDSIZE == 64 #  define LONG_MAX    9223372036854775807L #  else #  define LONG_MAX    2147483647L #  endif #  define LONG_MIN      (-LONG_MAX - 1L)```
The standard also defines the minimum values for each basic data type, but the majority of modern compilers exceed those requirements.
• 09-13-2005
scuzzo84
Sorry but I failed to understand your answer, what is a macro, and a header? The part in my book that talks about this problem I am having is the 50th page. I feel that you gave me a super complex example to a newb question.
• 09-13-2005
Thantos
A header is another file that brings predeveloped code into your program.

A macro is kinda complex to explain at your stage but lets just say if you were to have a INT_MAX in your program the compiler would replace it with whatever the max value for a signed integer was.
• 09-13-2005
cwr
Quote:

Originally Posted by scuzzo84
say I had an int for example

int dogs = 10000000;

is this supposed to be just int, or long?

The C standard only guarantees that an int will hold values from -32767 to 32767, so you should use long. Some implementations may define and document that an int can hold larger values, but then you are relying on implementation defined behaviour. For maximum portability, you need long.