C fixed size integer typedef declaration

Is there a reliable way to declare typedefs for integer types of fixed 8,16,32 and 64 bits in the ISO C standard

When I say the ISO C standard, I mean that strictly:

  • ISO C89 / C90, not C99.
  • There are no titles not defined in the ISO standard.
  • There are no preprocessor characters not defined in the ISO standard.
  • The ISO standard does not specify tolerances for standard sizes.
  • The provider’s own characters are missing.

I see other questions like this one in StackOverflow, but so far there are no answers that do not violate one of the above restrictions. I'm not sure if this is possible without resorting to platform symbols.

+4
source share
5 answers

Yes

The limits.h header file must be part of C90. Then I would check the SHRT_MAX , INT_MAX , LONG_MAX and LLONG_MAX values ​​according to the preprocessor's instructions and set the typedefs accordingly.

Example:

 #include <limits.h> #if SHRT_MAX == 2147483647 typedef unsigned short int uint32_t; #elif INT_MAX == 2147483647 typedef unsigned int uint32_t; #elif LONG_MAX == 2147483647 typedef unsigned long uint32_t ; #elif LLONG_MAX == 2147483647 typedef unsigned long long uint32_t; #else #error "Cannot find 32bit integer." #endif 
+8
source

Strictly speaking, ISO 9899: 1999, revised ISO 9899: 1990, is the only current language specification of the ISO standard C.

As an exact typedef width, names for integer types were introduced only in the standard in the 1999 version, what you want is impossible using only the standard version of the 1990 standard.

+6
source

No. There is a reliable way to declare individual integer variables up to 32 bits in size, however, if you are willing to live with some restrictions. Just use the long bit fields (the latter must be guaranteed to be at least 32-bit, and you are allowed to use up to a few bits in the bit fields, as if fit into a variable if the bitfield declarator was omitted). So:

 struct { unsigned long foo : 32; } bar; 

Obviously, you get all the restrictions that come with this, for example, the inability to have pointers to such variables. The only thing you really buy is a guaranteed workaround at the specified border for overflow / underflow, and even then only for unsigned types, since overflow is undefined for signed ones.

In addition, there is no portable way to do this in a pure C90. Among other things, the implementation of the Cant-compatible C90 does not even require an 8-bit integer - it is logical to have a platform in which sizeof(char) == sizeof(short) == sizeof(int) == 1 and CHAR_BIT == 16 ( i.e. it has a 16-bit machine word, and cannot address individual bytes). I heard that such platforms really exist in practice in the form of some DSPs.

+3
source

No, you cannot do this.

Now, if you want to consider a multi-step configure process, such as Gnu configure , as a solution, you can do this and stick with C89. And there are, of course, various types that you can use that are on C89, and this will be DTRT for almost every implementation that exists today, so you get the required sizes and adhere to pure C89 compliance. But the width of the bits, and what you want, usually will not be indicated by the standard.

0
source

The danger of such approaches when using modern compilers is that it has become fashionable for compilers to assume that a pointer of one integer type will not be used to access the values ​​of another, even if both types have the same size and presentation. If two types have the same size and presentation, and two parts of the same program select one of them, applying connection time optimization to programs that share pointers to such data can lead to incorrect behavior. For some implementations, many systems will have at least one integer size for which it will not be possible to declare a pointer that can be safely used to access all integer values ​​of that size; for example, on systems where both long and long long are 64 bits, there will be no way to declare a pointer that can be reliably used to access data of any type interchangeably.

0
source

All Articles