Why the infinite loop when data type is unsigned int?

后端 未结 3 1458
萌比男神i
萌比男神i 2020-12-12 05:42

The below code runs perfectly.Gives the correct output but, the moment I change the sign of the variables from signed to unsigned the program runs into an infinite loop. The

3条回答
  •  北海茫月
    2020-12-12 06:25

    The problem is that,

    for(i=count-1;i>=0;--i)
    

    Will never exit if i is unsigned. Because i is unsigned it will always be greater than or equal to zero, and thus the loop can't end.

提交回复
热议问题