+ 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 );
20 Answers
+ 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
+ 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).
+ 5
Quantum and Ong'ondi Eugene When printing an unsigned value, use the %u format. Using %d converts it to signed.
+ 4
Steve
That's what I said "unsigned int holds only 0 and positive numbers'.
+ 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.
+ 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.
+ 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 );
+ 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
+ 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?
+ 1
It actually is not negative. I should use the unsigned int format specifier, ud, in printf.
Thanks!
+ 1
Oops, sorry. Didn't see your second response, and that you figured that out for yourself.
+ 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
+ 1
Steve Yes, that's right. I forgot that.
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.
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.
0
unsigned seems to have no effect here as it prints out -2
unsigned int i = 0;
i--;
i--;
printf("%d", i);
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
0
Магистр техники и технологии. 111Стюра099 I'm not sure what is happening, but you're comment and code display all mangled up (unreadable)
- 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.
- 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.