When a C or C ++ programmer (hereinafter referred to as the second person) chooses the size of an integer variable, it is usually executed in one of the following cases:
- You know (at least roughly) the allowable range for a variable based on the value of the real world that it represents. For example,
numPassengersOnPlane in the airline reservation system should accommodate the largest supported aircraft, so at least 10 bits are required. (Round to 16.)numPeopleInState in the US tabulation program needs to accommodate the most populous state (currently about 38 million), so at least 26 bits are required. (Round to 32.)
In this case, you need the semantics of int_leastN_t from <stdint.h> . It is common for programmers to use the exact width of intN_t here, when technically they shouldn't; however, 8/16/32/64-bit machines are so prevailing today that the difference is simply academic.
You can use standard types and rely on restrictions like " int must be at least 16 bits", but the disadvantage of this is the lack of a standard maximum size for integer types. If an int is 32 bits, when you only need 16, then you have unnecessarily doubled the size of your data. In many cases (see below) this is not a problem, but if you have an array of millions of numbers, you will get many page errors.
- Your numbers do not need to be so large, but for efficiency reasons, you need a fast, โnativeโ data type, not a small one, which may require time spent on bitmasking or zero / sign expansion.
These are int_fastN_t types in <stdint.h> . However, as a rule, it is simple to use the built-in int here, which in 16/32-bit days had int_fast16_t semantics. This is not a native type on 64-bit systems, but it is usually good enough.
- A variable is the amount of memory, the index of the array, or the cast pointer, and therefore a size is required depending on the amount of address memory.
This corresponds to typedefs size_t , ptrdiff_t , intptr_t , etc. You should use typedefs here, because there is no built-in type that is guaranteed to have a memory size.
- This variable is part of a structure that is serialized to a file using
fread / fwrite or called from a non-C language (Java, COBOL, etc.), which has its own fixed-width data types.
In these cases, you really need the exact width type.
- You just did not think about the corresponding type and use
int out of habit.
Often this works quite well.
So, in general, all typedefs from <stdint.h> have their own use cases. However, the usefulness of built-in types is limited due to:
- Lack of maximum sizes for these types.
- Lack of memsize type.
- Arbitrary choice between LP64 (on Unix-like systems) and LLP64 (for Windows) data models on 64-bit systems.
Due to the fact that there are so many redundant typedefs of fixed width ( WORD , DWORD , __int64 , gint64 , FINT64 , etc.) and memsize ( INT_PTR , LPARAM , VPTRDIFF , etc.), mainly because <stdint.h> came at the end of C development, and people still use old compilers that don't support it, so libraries must define their own. For the same reason why C ++ has so many string classes.