Serge Ballesta
Serge Ballesta

Reputation: 149185

How to check In C language whether an int variable contains a legal (not trap representation) value?

Context:

This is a followup to that other question of mine. I asked about both C and C++ and soon got an answer about C++ because last draft for C++20 explicitly requires that signed integer types use two's complement and that padding bits (if any) cannot give trap representations. Unfortunately this is not true for C.

Of course, I know that most modern system only use 2-complement representations of integers and no padding bits, meaning that no trap representation can be observed. Nevertheless the C standard seem to still allow for 3 representations of signed types: sign and magnitude, one's complement and two's complement. And at least C18 draft (n2310 6.2.6 Representations of types explicitly allows padding bits for integer types other that char. This is still true for the latest version (n2454) I could find

Question

So in the context of possible padding bits, or non two's complement signed representation, int variables could contain trap values for conformant implementations. Is there a reliable way to make sure that an int variable contains a valid value?

Upvotes: 2

Views: 117

Answers (0)

Related Questions