Unsigned integers in C

While I run the program below, it outputs such as 109876543210-1-2-3-4-5-6-78-9-10-11-12- and s0 on. Why is that? What is the concept of an unsigned whole?

 main ()
   {
      unsigned int i;
       for (i = 10; i >= 0; i--)
                 printf ("%d", i);
   }
+5
source share
9 answers

Unsigned integers are always non-negative, that is, greater than or equal to 0. When you subtract 1 from 0 in an unsigned integer type, you get MAX_INT.

Therefore, the for loop will never be completed.

However, you need to use "% u" not "% d" in your printf if you want it to print an unsigned value, not a signed value.

+4
source

The format string %dprocesses the substituted value as intwhich is signed.

+2

?

undefined ( %d, int unsigned) - , - .

? unsigned int >= 0.

?

... . , .

+2

%d, printf . ( >= 0) .

, 10 0, (109876543210). ( , 32bit int 0xFFFFFFFF). 0 , . printf 0xFFFFFFFF -1, %d. 0xFFFFFFFE, -2, >= 0 unsigned.

%u.

+1

printf , , , , - ( ). , , %d .

+1

"% d" printf(), .

0

%u printf, int.

0

unsigned integer . , >> vs, ( , ).

, %u.

0

( 16-) -32768 32767 ( 0x8000 0x7FFF), - 0 65535 ( 0x0000 0xFFFF). , , . , , (%d), , . - , , ... unsigned int.

0

All Articles