I assume that you are asking this question, because at some point you do not know the specific type of your variable foo , otherwise you will naturally use UINT_MAX , etc.
For C, your approach is only valid for types with a conversion view of int or higher. This is due to the fact that before the comparison, the unsigned short value, for example, is first converted to int if all values โโmatch, or to unsigned int otherwise. So, your foo value will be compared with either -1 or UINT_MAX , and not with what you expect.
I don't see a simple way to run the test you want in C, since basically using foo in any type of expression will contribute to its int .
With the gcc typeof extension, this is easily possible. You just need to do something like
if (foo == (typeof(foo))-1)
source share