Thread: unsigned int and unsigned long

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    29

    unsigned int and unsigned long

    Hello
    I want to know what is the difference between
    unsigned long and unsigned int ?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    unsigned int potentially represents a smaller maximum value than does unsigned long.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    But both unsigned int and unsigned long have the same 4 byte of memory
    what are the values that each of them types can get ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by codewriter
    But both unsigned int and unsigned long have the same 4 byte of memory
    Your assertion is not guaranteed to be true.

    Quote Originally Posted by codewriter
    what are the values that each of them types can get ?
    unsigned int is guaranteed to have a range that is at least [0, 65535]. For unsigned long this minimum range is [0, 4294967295].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    So what you saying is that unsigned int is a short type that take just 2 bytes in the memory ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by codewriter
    So what you saying is that unsigned int is a short type that take just 2 bytes in the memory ?
    Maybe. It might take 4 bytes, or it might just take 1 byte for an unusual system that has 16 bit bytes. Basically, these are minimum guarantees; beyond that it is implementation defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    I am using a GNU C Compiler. when i write this in my code :

    int tmp;
    is temp getting 2 bytes or four ?
    what is the maximum value that tmp can get in this matter ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by codewriter
    I am using a GNU C Compiler. when i write this in my code :

    int tmp;
    is temp getting 2 bytes or four ?
    Answer your question yourself by checking the value of sizeof(temp) (or sizeof(tmp), as the case may be).

    Quote Originally Posted by codewriter
    what is the maximum value that tmp can get in this matter ?
    #include <limits.h> and check the value of UINT_MAX.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    Hi Laserlight , Your assertion is not guaranteed to be true!
    Both unsigned int and unsigned long generating a range value of 4G. and both of them get an 4
    byte of memory.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by codewriter
    Hi Laserlight , Your assertion is not guaranteed to be true!
    Both unsigned int and unsigned long generating a range value of 4G. and both of them get an 4
    byte of memory.
    If you are asserting that my assertions are not guaranteed to be true, I can guarantee you that you did not read my assertions correctly
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    The GNU compiler will not take you "1 byte implementation defined " for unsigned int - the compiler will throw
    you an error !

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by codewriter
    The GNU compiler will not take you "1 byte implementation defined " for unsigned int - the compiler will throw
    you an error !
    Hmm... maybe I should have explained what we mean by the "implementation": basically, the "implementation" refers to either the compiler or the standard library implementation, in the context of the operating system or whatever is the relevant environment.

    So, with your compiler, the implementation has set each byte to be 8 bits, and set an unsigned int to be 4 bytes (hence 32 bits). It follows that the range of an unsigned int will be [0, 4294967295]. On another compiler (or even just gcc on a different platform), things might be different. However, it cannot be different such that the minumum guarantees are violated, e.g., it cannot be the case (for a standard conforming implementation) that an unsigned int has the range [0, 255]. But it could be different such that an unsigned int has the range [0, 18446744073709551615].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't extrapolate universal "truth" from experiments on one compiler implementation.

    The standard specifies MINIMUM (not maximum or absolute) ranges for each data type.

    The standard says that unsigned int should be able to represent [0, 65535].
    The fact that your compiler supports unsigned int over [0, 4294967295] just means it matches the specification.

    It does NOT prove that ALL unsigned int's are the same as on your machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    29
    I understand you both very well now. Thank You

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    short int <= int <= long int
    float <= double <= long double

    I have never encountered a machine/compiler that had (short) as anything but 2 bytes.
    (int), however, has been 2 bytes is the old days, and currently seems to be 4 bytes even on so-called 64-bit machines. This is truly the system dependent one. I bet embedded controllers still a the smaller size for this.
    (long) has been 4 bytes forever. I don't see this one changing anytime soon.
    (long long) is 8 bytes since it's been introduced. Notice none of the (int) or (long) has increased even in current 64-bit environments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  2. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. unsigned int and unsigned long int
    By -EquinoX- in forum C Programming
    Replies: 43
    Last Post: 03-05-2008, 05:17 PM
  5. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM