+ 10

Why is the output 1?

This is a code snippet from a C challenge. Should this not be an infinite loop? It runs only once. If i print out the variabel i it have the value 0. What make the loop to end? unsigned int i; int count =0; for (i=0; i<10; i--) count++; printf ("%d", count );

24th Oct 2021, 8:55 AM
Adam Aksu
Adam Aksu - avatar
20 Réponses
+ 6
Adam Aksu and A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ An unsigned value in C can never be negative. In two's complement representation subtracting one from zero results in a very large number. See https://en.m.wikipedia.org/wiki/Two's_complement
24th Oct 2021, 10:15 AM
Steve
Steve - avatar
+ 5
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Sorry, I missed where you said that. But I did want to make it clear that the loop ends because i becomes a large number (much greater than 10).
24th Oct 2021, 10:46 AM
Steve
Steve - avatar
+ 5
Quantum and Ong'ondi Eugene When printing an unsigned value, use the %u format. Using %d converts it to signed.
25th Oct 2021, 2:44 AM
Steve
Steve - avatar
+ 4
Steve That's what I said "unsigned int holds only 0 and positive numbers'.
24th Oct 2021, 10:20 AM
A͢J
A͢J - avatar
+ 3
Steve, yes that make sense. But still when printing out the value outside the loop it has value -1. That's what puzzles me.
24th Oct 2021, 11:01 AM
Adam Aksu
Adam Aksu - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ But if i is unsigned then how come it get a negative value? Plus it is still less than 10.
24th Oct 2021, 9:07 AM
Adam Aksu
Adam Aksu - avatar
+ 2
Or you could have incremented it to make count to reach ten because it will incrementing unsigned values unsigned int i; int count =0; for (i=0; i<10; i++) count++; printf ("%d", count );
24th Oct 2021, 8:35 PM
Ong'ondi Eugene
Ong'ondi Eugene - avatar
+ 2
Type of variable unsigned int: represents a positive integer. Depending on the processor architecture, it can take 2 bytes, (16 bits), or 4 bytes, (32 bits), and because of this, the range of limit values of is 0 to 65 535, (for 2 bytes), or 0 to 4 294 967 295, (for 4 bytes). For example: unsigned int unInt = 2; printf("%u", unInt-=3); Output: 4 294 967 295 (for 4 bytes) since the range of values goes in a circle ...4 294 967 295, 0, 1, 2... https://code.sololearn.com/c2y5Ph1cyuYZ/?ref=app
25th Oct 2021, 11:53 PM
Solo
Solo - avatar
+ 1
Thanks A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ . But if unsigned int can only hold positive numbers and zero how can it get -1? Should it not flip over to the highest int?
24th Oct 2021, 9:28 AM
Adam Aksu
Adam Aksu - avatar
+ 1
It actually is not negative. I should use the unsigned int format specifier, ud, in printf. Thanks!
24th Oct 2021, 11:21 AM
Adam Aksu
Adam Aksu - avatar
+ 1
Oops, sorry. Didn't see your second response, and that you figured that out for yourself.
24th Oct 2021, 5:56 PM
Steve
Steve - avatar
+ 1
Because the first loop will be through and count becomes 1, the next round makes it false because you declared I to be unsigned so the loop ends and the final results of I becomes only one
24th Oct 2021, 8:33 PM
Ong'ondi Eugene
Ong'ondi Eugene - avatar
+ 1
Steve Yes, that's right. I forgot that.
25th Oct 2021, 5:11 AM
Jan
Jan - avatar
0
Because of i-- and i is unsigned int so after first iteration i will be -1 and count will be 1 If there is no unsigned int then there would be no output because of infinite loop.
24th Oct 2021, 8:58 AM
A͢J
A͢J - avatar
0
Adam Aksu That's because you're converting it back to a signed value with the format option, d, in the printf(). Use u if you want to see the unsigned value.
24th Oct 2021, 5:54 PM
Steve
Steve - avatar
0
unsigned seems to have no effect here as it prints out -2 unsigned int i = 0; i--; i--; printf("%d", i);
24th Oct 2021, 7:30 PM
Jan
Jan - avatar
0
If u want an infinite loop, this one's better and small👇 int i; for(i=1;i>0;i++) printf("%d\n",i-1); It will basically run infinitely printing number from 0 to infinity
25th Oct 2021, 10:52 AM
Jolen Mascarenhas
0
Магистр техники и технологии. 111Стюра099 I'm not sure what is happening, but you're comment and code display all mangled up (unreadable)
15th Dec 2021, 10:42 PM
Steve
Steve - avatar
- 3
Adam Aksu unsigned int can only hold 0 and positive numbers so after first iteration count is 1 and i is -1 Since unsigned int can only hold positive numbers so loop will stop there for further execution.
24th Oct 2021, 9:11 AM
A͢J
A͢J - avatar
- 3
Adam Aksu unsigned int holds only 0 or positive numbers it doesn't mean i will not be -1 I would be -1 but unsigned int can't hold it so loop be break there.
24th Oct 2021, 9:41 AM
A͢J
A͢J - avatar