Reputation: 41
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
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
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:
uint64_t
is best.uint_least64_t
is best.uint_fast64_t
is best.uint_least64_t
or uint_fast64_t
is best, according to whether a secondary concern is space or time, respectively.Upvotes: 1