Find errors in the following code block


Image that you have the following code block:


unsigned int i; for (i = 100; i >= 0; --i) printf("%d\n", i);


The mentioned above code block has two errors.


The first error is that we use the unsigned int type, which only works with values ​​greater than or equal to zero. Consequently, the for loop condition will always be true and we will face the infinite loop.


A valid code that outputs all the numbers from 100 to 1 should use the condition i > 0. In case you want to output a zero value, then an additional printf statement should be added after the for loop.



nsigned int i;

for (i = 100; i> 0; --i)

    printf ("% d \ n", i);

printf ("% d \ n", i);


The second error is that you need to use %u instead of %d, since we print integer values ​​without a sign.


unsigned int i;

for (i = 100; i> 0; --i)

    printf ("% u \ n", i);


Finally, this code block correctly outputs a list of numbers from 100 to 1, in descending order.

Leave a Comment