0

Possible Duplicate:
Why are C++ int and long types both 4 bytes?

In C/C++, what is the difference between:

u_int64 myNum;

and:

unsigned long myNum;

As far as I can tell, both ate just unsigned integers, with 64bits of memory.

Community
  • 1
  • 1
Christian Daley
  • 779
  • 2
  • 9
  • 13

3 Answers3

8

unsigned long does not have to be 64 bits, whereas uint64_t does. There is a kind of hierarchy of integer types where each type has to be at least large as the preceding type: signed char, short, int, long, long long, and similarly for their unsigned counterparts. There are some anchor points, stating that char is one byte (a byte does not have to be 8 bits, as far as I can recall short is at least 2 bytes 16 bits. In C++11, long long is at least 64 bits. But none of these types is exactly a given number of bits.

See fixed width integer types for more information (thanks to @chris for the link).

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • So if I want a number that is always 64bit, I should use u_int64 – Christian Daley Jan 16 '13 at 22:32
  • @ChristianDaley, See [here](http://en.cppreference.com/w/cpp/types/integer). It might help you. – chris Jan 16 '13 at 22:34
  • 1
    @ChristianDaley Do you mean `uint64_t`? If so, yes. However, the type may not exist if your platform does not provide a 64 bit type. There's always `uint_least64_t` which will provide the first type that has at least 64 bits. – Joseph Mansfield Jan 16 '13 at 22:34
  • Your hierarchy should begin with `signed char`. The naked `char` is a bit of an odd one out. – Kerrek SB Jan 16 '13 at 22:39
  • It's entirely feasible for `short` to be only one byte long. In fact, you can have `sizeof(char) == sizeof(short) == sizeof(int) == 1`, as long as a `char` is wide enough. – Kerrek SB Jan 16 '13 at 22:39
  • @KerrekSB thanks for the `signed char` correction. Concerning `short`, could it be that it is "at least 16 bits", not two bytes? O rmaybe I dreamt the whole thing up. – juanchopanza Jan 16 '13 at 22:41
  • @juanchopanza: Right, `short` needs at least 16 bits. – Kerrek SB Jan 16 '13 at 22:48
  • `There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list.` - § 3.9.1 – David Jan 16 '13 at 22:57
0

The C language itself doesn't specify how big an int is

long only has to be at least as long as int
long long only has to be at least as long as long

also u_int64_t is a c99 type, and wouldn't be available in ANSI c89.

also among 64 bit architectures that are differences LP64 indicates long's and pointers are 64 bit, LLP64 means that long long's and pointers are 64 bit.

Grady Player
  • 14,399
  • 2
  • 48
  • 76
0

unsigned long is dependent on the machine like every int in C/C++. Many library especially libraries that allow for two machines to interact will type def most int like numbers to make sure both are the same size. u_int64 is basically type def to an unsigned integer of 64 bits, to allow for use on any machine. In theory unsigned long could be 128, 64, 256 or pretty much any size.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57