问题
Failing to get a detailed answer to my question here. I thought I would tackle it from a different angle.
Would someone be able to explain what selection criteria are used for determining the underlying types for C99's fixed-width integer types:
[u]int_fast[n]_t
[u]int_least[n]_t
[u]int[n]_t
For a given processor, if 'long' and 'int' are the same size (sizeof(int) == sizeof(long)) then why would 'long' be used over 'int' or vice versa.
回答1:
The whim of the author of <stdint.h>
.
Given that int
and long
are the same size (and assuming they have the same representation and other characteristics), it shouldn't matter at all which of them is used to define [u]int_{,_fast,_least}32_t
, as long as the type or types satisfy the requirements of the standard.
Well, that's not quite true; it can make a difference in some cases. int
and long
, even if they're the same size, are still distinct and incompatible types. For example, given that int32_t
is typedef
ed either as int
or as long
, the following program:
#include <stdint.h>
#include <stddef.h>
int main(void) {
int32_t *p32 = NULL;
int *ip = p32;
long *lp = p32;
return 0;
}
violates a constraint and requires a diagnostic either on the declaration of ip
or on the declaration of lp
, depending on how int32_t
is defined. But you should avoid writing such code anyway.
来源:https://stackoverflow.com/questions/11518212/c99-s-fixed-width-integer-types