3

I just wanted to know, Why we need to have uint64_t which is actually a typedef of unsigned long , when unsigned long is anyway available. Is it only for make the name short or any other reason ?

Chinna
  • 3,930
  • 4
  • 25
  • 55

5 Answers5

9

The reason should be pretty obvious; it's a guarantee that there is 64 bits of precision. There is no such guarantee for unsigned long.

The fact that on your system it's a typedef, just means that on your system unsigned long is 64 bits. That's not true in general, but the typedef can be varied (by the compiler implementors) to make the guarantee for uint64_t.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

It's not always a typedef for unsigned long, because unsigned long is not universally 64 bit wide. For example, on x64 Windows, unsigned long is 32 bit, only unsigned long long is 64 bit.

1

Because uint64_t means a type of exactly 64 bits, where unsigned long can be 32 bits or higher.

On a system where unsigned long is 32 bits, it would be used to typedef uint32_t , and unsigned long long would be used to typedef uint64_t.

2501
  • 25,460
  • 4
  • 47
  • 87
1

Yes, it does make it shorter, but that's not the only reason to use it. The main motto of using this typedef-ed data types is to make a code more robust and portable accross various platforms.

Sometimes, unsigned long may not be 64 bits, but a definition of uint64_t will always gurantee 64 bit precision. The typedef of uint64_t can [ read as : will] be varied accross different platform to have a precision of 64 bits.

Example:

  1. for systems having long as 32 bits, uint64_t will be typedef to unsigned long long
  2. for systems having long as 64 bits, uint64_t will be typedef to unsigned long
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Not only to make it shorter, but also to make it more descriptive.

If you see in code uint64_t it will be easy to read as unsigned int of 64 bits data type.

bitcell
  • 921
  • 8
  • 16