Why is the output 1? | Sololearn: Learn to code for FREE!

+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 );

c

10/24/2021 8:55:42 AM

Adam (Aydin) Aksu

24 Answers

New Answer

+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

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.

+1

Oops, sorry. Didn't see your second response, and that you figured that out for yourself.

+1

unsigned seems to have no effect here as it prints out -2 unsigned int i = 0; i--; i--; printf("%d", i);

+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.

+1

Alhamdulilah

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

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

Adam Aksu After reading all answers: We come to know that I became larger than 10. Ok fine Let print "i" value and try to figure out why that particular value. I'm trying too...😊

0

Adam Aksu // Why count is equal to one. #include <stdio.h> int main() { unsigned int i; int count = 0; for(i=0; i<10; i--) { count++; } printf ("count = %d\n\ni = %u \n\n\-1 = %u", count ,1,-1); return 0; } // OUTPUT count = 1 i = 4294967295 -1 = 4294967295