Game changer
Game changer

Reputation: 41

What's the best datatype in c that allows me to store a 64 bit natural number?

I would like to know the best datatype, in c, that would allow be to store on it a NATURAL 64 bit number.

Upvotes: 1

Views: 275

Answers (2)

Krishna Kanth Yenumula
Krishna Kanth Yenumula

Reputation: 2567

The size of data type depends on the different things, such as system architecture, compiler, and even the language. In 64 bit systems,unsigned long, unsigned long long is of 8 bytes , so it can hold 64 bit natural number (always positive).

Please visit this link : Size of data type
From <stdint.h>,

#if __WORDSIZE == 64
typedef unsigned long int     uint64_t;
#else
__extension__
typedef unsigned long long int    uint64_t

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 223795

“Natural number” may mean either positive integer or non-negative integer.

For the latter, the optional type uint64_t declared in <stdint.h> is an exact match for a type that can represent all 64-bit binary numerals and takes no more space than 64 bits. Support for this type is optional; the C standard does not require an implementation to provide it. There is no exact match for the former (positive integers), as all integer types in C can represent zero.

However, the question asks for the “best” type but does not define what “best” means in this question. Some possibilities are:

  • If “best” means a type that can represent 64-bit binary numerals and does not take any more than 64 bits, then uint64_t is best.
  • If “best” means a type that can represent 64-bit binary numerals and does not take any more space than other types in the implementation that can do that, then uint_least64_t is best.
  • If “best” means a type that can represent 64-bit binary numerals and is usually fastest, then uint_fast64_t is best.
  • If “best” means a type that can represent 64-bit binary numerals and is definitely supported, then either uint_least64_t or uint_fast64_t is best, according to whether a secondary concern is space or time, respectively.

Upvotes: 1

Related Questions