Question

Is it possible to terminate a while() loop with an unsigned int? For example I want to terminate a while() when the user enters a negative value. But I want it to be any negative value, not just -1.

Was it helpful?

Solution

Not that I think this is a good idea but, in C at least, you can check if your unsigned integer is greater than INT_MAX (for two's complement anyway, not so sure about the sign/magnitude and one's complement variants but they're probably rare enough that you could safely ignore them until a problem pops up).

This is, of course, assuming it was read in as an integer and converted to unsigned somewhere - if you use customised input routines expecting only unsigned numbers, they may barf at the presence of a leading - sign.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top