long int vs int

This is a discussion on long int vs int within the C Programming forums, part of the General Programming Boards category; Hi, I was running through the integer variable types yesterday and despite re-reading the details I cannot understand the need ...

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    long int vs int

    Hi,

    I was running through the integer variable types yesterday and despite re-reading the details I cannot understand the need for both int and long int. What I mean is, if int provides the same range of values as long int, then why have both at least for my version? I understand that on some machines the size of int does differ.

    Code:
    Type   Number (bytes)  Range of Values
                  
    signed char   1        128 to +127
    short int     2        32,768 to +32,767
    int           4        2,147,438,648 to +2,147,438,647
    long int      4        2,147,438,648 to +2,147,438,647
    long long int 8        9,223,372,036,854,775,808 to +9,223,372,036,854,775,80
    
    Similarly, for unsigned....
    unsigned int     4       0 to 4,294,967,295
    unsigned long    4       0 to 4,294,967,295

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    int is the default integer type supported by your compiler, and is typically compatible with your hardware and/or operating system.

    There is actually no requirement that an int be the same size as a long int, although it often works out that way. There are some real-world compilers for which an int is the same size as a short int, and the long int supported by those compilers is actually bigger than an int (takes more storage, can hold larger values).

    The actual rule is that all values that can be stored in a short int can also be stored in an int, and all values that can be stored in an int can also be stored in a long int. The reverse is not true: there is no requirement that every value that can be stored in a long int can also be stored in an int (although there is nothing preventing it either).

    In practice that translates to a guideline that sizeof(short int) <= sizeof(int) <= sizeof(long int).

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by grumpy
    There is actually no requirement that an int be the same size as a long int, although it often works out that way. There are some real-world compilers for which an int is the same size as a short int, and the long int supported by those compilers is actually bigger than an int (takes more storage, can hold larger values).
    I agree with what you say and most books point this out. What I am trying to confirm is that in my particular example (compiler/machine) and circumstance - there is no difference in storage size between long int and int (again for my particular example) ? Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Yes, with your particular compiler there is no difference between long int and int (except that, some compilers will give warnings about suspicious conversions if you pass an int to a function expecting a long int, even if it is true int and long int are the same size for that compiler). The reason is that a compiler is still required to support types short int, int, long int, even if two of them may correspond to the same thing. If you assume in code that int and long int are the same size, you will introduce a major problem for some person in the future (possibly yourself) who has to port your code to a compiler where int and long int are of different sizes.

  5. #5
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Thank you for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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