TITLE: specific bit sized datatypes (Newsgroups: comp.std.c++, 16 Mar 98) [ Although this is really a C question, it should be of interest to C++ developers. Note that some of this is addressed with numeric_limits<> (search the cpptips site for numeric_limits to see the specific tips. -adc ] WHITE: White > Is there anything in the the ANSI C++ spec that provides for datatypes of > known bit sizes? > > If not, could such a thing be added? Such a thing is really needed when > making data structures that can be moved across machines. > > In the past, I've used the following conventions and found they work > very well. > > typedef UINT32 ...; // an unsigned integer of _exactly_ 32 bits > typedef SINT32 ...; // a signed integer of _exactly_ 32 bits > typedef uint32 ...; // an unsigned integer of _at_least_ 32 bits > typedef sint32 ...; // a signed integer of _at_least_ 32 bits > > The "uint32" would be defined as a the type optimial for that compiler > that was a minimum of 32 bits long. A "uint8" would probably be an > "unsigned char" while "sint16" and "sint32" would just be "int" (on a > 32-bit machine, of course). > > Of course, there are UINT16, SINT8, uint64, etc., as well. I've also > defined FLOAT32, FLOAT64, float32, and float64 types using the same > conventions. JOHN: john@mail.interlog.com (John R MacMillan) [snip] To stick to the question, as near as I can see, the draft standard does not include such things, but the C9X draft for the next revision of the C Standard does, and it may eventually get folded into C++. The C9X proposal adds a new header, inttypes.h, that typedefs at least, fastest at least, and exact sized integers of various sizes, as well as largest integer types, and types for pointer/int conversion. There are also macros that are defined that can be used to check if the required type exists. Also, if my reading of the draft is correct, there is still the caveat that integer types are still allowed to have `holes' so the exact sized types may not actually be the size you expect, they just have to _behave_ as if they are. KUYPER: James Kuyper The new standard header for C9X will include things like this (but only for integers). The naming is a little clumsy: int8_t exactly 8 bits uint_least16_t at least 16 bits int_fast32_t Fastest integral type big enough to hold 32 bit integers intmax_t Largest integer type intptr_t A type large enough to safely store and return a void * There are corresponding macros for each type: INT64_MAX INT_LEAST_32_MIN UINT16_C() expands its argument into a integer constant of the specified type, by adding suffixes as necessary. PRIiFAST64 printf() format string SCNd16 scanf() format string Only the 8, 16, 32, and 64 sizes are required, and all exact and pointer types are optional. You can test for the existence of the optional types by using #ifdef of a corresponding macro. The C9X standard has reserved the entire [u]int*_t name space, plus the corresponding macro names for future library extensions. It is probably too late add these to the C++ standard, but I think you will find that most compiler makers who bundle together C and C++ compilers will provide these types as extensions in the C++ compiler. almost certainly will be considered for inclusion in the following C++ standard, many years from now.