Is this an infinite loop? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Is this an infinite loop?

Explain !!!.... unsigned int means only positive values right Please explain šŸ¤”šŸ¤”šŸ¤”šŸ¤” https://code.sololearn.com/cd9qc0I2NRXv/?ref=app

17th May 2020, 4:26 AM
Rasik Raikar
Rasik Raikar - avatar
5 Respostas
+ 2
Unsigned meaning positive value only so when we do i-- it will take the value on other side of range which is always positive or equal to zero so the condition will get always satisfied and the loop will run infinite times
19th May 2020, 5:51 AM
Aayushi Amonkar
Aayushi Amonkar - avatar
+ 3
Yes, due to unsigned integer overflow. When subtracting from 0 with a unsigned int the result overflows back to the upper limit of an unsigned int. Where unsigned int range is 0-Max, 0 - 1 = Max. Try the following code unsigned int a = 0; a--; printf("%u", a);
17th May 2020, 4:46 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Try yourself! Instead of printing "a" just printf("%x ", i) What happens? Instead of evolving from 0 to -1, i evolves from 0 to ffffffff... In the signed case, ffffffff is by convention taken as -1 and the loop stops. In the unsigned case, i will never be considered negative and the loop will never stop.
17th May 2020, 4:43 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Thk u
17th May 2020, 7:42 AM
Rasik Raikar
Rasik Raikar - avatar
+ 1
Thk u Aayushi šŸ˜šŸ’™
19th May 2020, 8:21 AM
Rasik Raikar
Rasik Raikar - avatar