Hello
I want to know what is the difference between
unsigned long and unsigned int ?
Thanks
Hello
I want to know what is the difference between
unsigned long and unsigned int ?
Thanks
unsigned int potentially represents a smaller maximum value than does unsigned long.
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 ?
Your assertion is not guaranteed to be true.Originally Posted by codewriter
unsigned int is guaranteed to have a range that is at least [0, 65535]. For unsigned long this minimum range is [0, 4294967295].Originally Posted by codewriter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by codewriter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 ?
Answer your question yourself by checking the value of sizeof(temp) (or sizeof(tmp), as the case may be).Originally Posted by codewriter
#include <limits.h> and check the value of UINT_MAX.Originally Posted by codewriter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 correctlyOriginally Posted by codewriter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.Originally Posted by codewriter
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].
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 understand you both very well now. Thank You
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.