Like Tree1Likes

unsigned int and unsigned long

This is a discussion on unsigned int and unsigned long within the C Programming forums, part of the General Programming Boards category; Hello I want to know what is the difference between unsigned long and unsigned int ? Thanks...

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

    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
    5,843
    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%.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    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
    20,968
    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].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    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
    20,968
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    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
    20,968
    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].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    I understand you both very well now. Thank You
    Salem likes this.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.

Page 1 of 2 12 LastLast
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, 04: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, 11:09 AM

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