开发者

Using an unsigned int to terminate a while() loop

开发者 https://www.devze.com 2023-04-11 08:13 出处:网络
Is it possible to terminate a while() loop with an unsigned int?For example I wa开发者_如何学运维nt to terminate a while() when the user enters a negative value.But I want it to be any negative value,

Is it possible to terminate a while() loop with an unsigned int? For example I wa开发者_如何学运维nt 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消