문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top