Reputation: 131970
Does the C99 standard mandate that a conforming compiler have a 64-bit int64_t
defined (and usable)? Or is it optional, and just happens to be defined by all popular compilers? I'm obviously asking specifically about platforms on which the CPU can't process 64-bit values directly, but the question is more general
I can't really figure this out from the C data types Wikipedia page, nor from this answer to a related question.
Upvotes: 2
Views: 571
Reputation: 131970
int64_t
type.(Thanks @user3386109, @Clifford)
An int64_t
type is not required to be available. Quoting the C99 standard draft document N1256:
7.18.11.1 Exact-width integer types
- The typedef name int N_t designates a signed integer type etc. etc. ...
- These types are optional. However, if an implementation provides integer types with widths of ... 64 bits... that have a two’s complement representation... it shall define the corresponding typedef name.
But see @chux and @JohnBollinger's answers about long long
having 64 bits.
Upvotes: 1
Reputation: 154315
Does the C99 standard mandate that a conforming compiler have a 64-bit int64_t defined (and usable)?
No, yet C99 requires long long
which is at least 64-bits.
Further, int64_t
is very commonly available. It would be very unlikely you will encounter a conformant C99 without int64_t
as it is required on nearly all platforms.
C11dr 7.20.1.1 Exact-width integer types
(int64_t
) .... if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.
The bit width of the processor is not a factor in this functionality - long long
must exist. If that long long
(or any standard type is 64-bit 2's compliment), then int64_t
must exist too. A processer's bit width does affect performance.
@R.. comment emphasizes that a long long
that takes 64-bits of memory to encode, can, by spec only support the range of [-0x7fff-ffff-ffff-ffff ... +0x7fff-ffff-ffff-ffff], one shy of int64_t
range of [-0x8000-0000-0000-0000 ... +0x7fff-ffff-ffff-ffff]. Such platforms these days are exceedingly rare, if they exist.
Upvotes: 2
Reputation: 180998
Does the C99 standard mandate that a conforming compiler have a 64-bit int64_t defined (and usable)? Or is it optional, and just happens to be defined by all popular compilers?
The type is optional, in one sense, and conditionally required in a different sense. Specifically, C99 says,
The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two's complement representation. [...]
These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.
Thus, int64_t
is optional in the sense that a conforming implementation is not required to provide any type that exactly matches the characteristics of an int64_t
, and if it doesn't, then it needn't (indeed, must not, according to another section) provide type int64_t
.
C99 does specify that there is a type long long int
whose required minimum range necessitates a representation at least 64 bits wide. Now it is possible that in some implementation there is no signed integer type exactly 64 bits wide (for example, maybe int
is 24 bits, long
48, and long long
96), and it is possible that there is a 64-value-bit integer type, but it contains padding bits or is not represented in two's complement. Such implementations could be fully conforming and yet not define an int64_t
. In practice, though, there aren't any such implementations in common use today.
Upvotes: 5
Reputation: 754470
There are three sets of integer types:
intN_t
— such as int64_t
and their unsigned counterparts; these exact types may not be available.int_leastN_t
— such as int_least64_t
; types int_least8_t
, int_least16_t
, int_least32_t
and int_least64_t
and their unsigned counterparts are required — other types are optional.int_fastN_t
— such as int_fast64_t
; types int_fast8_t
, int_fast16_t
, int_fast32_t
and int_fast64_t
are required (they're the fastest type with at least the given width).The standard also requires support for long long
, and the minimum acceptable range for long long
requires at least 64 bits (see §5.2.4.1 Sizes of integer types <limits.h>
). Therefore, the standard can legitimately demand support for the 'least' and 'fast' types at 64 bits or more — it also requires that to support long long
.
There used to be computers with 36-bit words, and others with 60-bit words. Both of these would struggle to provide (basically, "couldn't provide") the exact width types, but can easily provide support for the 'least' and 'fast' types.
The standard does not mandate the 'exact width types' — see §7.20.1.1 Exact-width integer types ¶3:
These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.
Upvotes: 2