Short/long/signed/unsigned int

This is a discussion on Short/long/signed/unsigned int within the C Programming forums, part of the General Programming Boards category; Im on a chapeter in my C book talking about Integer Types. It talks a bit about Short/long/signed/unsigned. For example ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    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;

    A friend also told me not to worry about this since computers now have enough ram, but I still want to understand this.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First rule of thumb: Never listen to friends

    In the header limits.h there are a whole bunch of macros designed to help you answer this very question

    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.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    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.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21