7

While i am running the program below it outputs like 109876543210-1-2-3-4-5-6-78-9-10-11-12-and s0 on. Why so? What is the concept of unsigned integer?

 main ()
   {
      unsigned int i;
       for (i = 10; i >= 0; i--)
                 printf ("%d", i);
   }
amitshree
  • 2,048
  • 2
  • 23
  • 41
  • Btw the difference between signed int and unsigned int in c is for example in the way it does bit shifts, which effectively means wheter it uses sal/sar or shl/shr instructions. Else the number in the register looks the same. – Pyjong Mar 11 '13 at 14:02
  • @stupid_idiot, also it affects comparison, multiplication (mul/imul), division (div/idiv) and propagation to larger types. Maybe something else, not quite sure. – Aneri Mar 11 '13 at 14:07
  • There is nothing wrong with this question so it should not be closed as "not a real question". But due to the very basic nature, perhaps it is "too localized". – Lundin Mar 11 '13 at 15:22

9 Answers9

9

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 end up with MAX_INT.

Therefore, your for loop will never terminate.

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

Vicky
  • 12,934
  • 4
  • 46
  • 54
2

The %d format string treats the substituted value as int, which is signed.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Why so?

Because your program has an undefined behavior (using %d which is for int to print an unsigned) - so you can expect anything to happen.

Why the infinite loop? Because an unsigned int is always >= 0.

What is the concept of unsigned integer?

It's an... unsigned integer. A whole number which is non-negative.

  • @Virgile There is no conversion from `unsigned` to `int` in the statement `printf("%d", u);` any more than there is a conversion from `float` to `int` in the statement `printf("%d", f);`. 6.3.1.3 does not apply, and 7.19.6.1:8 says that the argument corresponding to `%d` should be an `int`. – Pascal Cuoq Mar 11 '13 at 16:20
  • For a normal function call, a conversion would occur, but you're right, variadic functions behave differently, and 7.19.6.1§9 makes it clear that this is UB. I'm thus deleting the original comment. – Virgile Mar 11 '13 at 17:08
1

You use %d, so printf interprets the value as signed. Comparison to zero (>=0) of unsigned is always true.

So, while values are from 10 to 0, the output is ok (109876543210). After that the value becomes huge positive (maximum value, for 32bit int it is 0xFFFFFFFF). Comparison to 0 is true, so the loop continues. But in printf 0xFFFFFFFF produces -1 since %d is used. Then the loop continues to 0xFFFFFFFE which is -2 but still >= 0 as unsigned.

Use %u.

Aneri
  • 1,342
  • 8
  • 21
  • yes here is this is the program and running on linux #include int main() { unsigned int i; for (i = 10; i >= 0; i--) printf ("%d", i); return 0; } – amitshree Mar 11 '13 at 14:00
1

printf can't know the type of the variable you give it, all it gets is the value (the bits themselves). You have to tell it how to interpret that value, and with %d you tell it to interpret it as a signed integer.

nemetroid
  • 2,100
  • 13
  • 20
0

You are using "%d" with printf() which is the format specifier for a signed integer.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
0

You should use %u as a format specifier in the printf, otherwise the value is casted to int.

Dan
  • 1,466
  • 1
  • 13
  • 27
0

The declaration unsigned integer instructs the compiler to use unsigned operations on the variable. For example, the >> operator has different behavior on unsigned integers vs signed (specifically, whether it should preserve the sign bit).

To print an unsigned integer, you should use the %u formatting.

onon15
  • 3,620
  • 1
  • 18
  • 22
  • when using '%u' in outputs like 10987654321042949672954294967294429496729342949672924294967291429496729... what does it it mean.i tried only to print from 10 to 0 unsigned numbers – amitshree Mar 11 '13 at 14:11
  • Since you have no delimiters between your prints, what you actually see there is the numbers - 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 4967295,4294967294,4294967293... the numbers after and including the 11th number are due to your exit condition not being filled (unsigned integers are always >=0, so when you decrease them from 0 they roll over - you can read about it [here](http://en.wikipedia.org/wiki/Integer_overflow)) – onon15 Mar 11 '13 at 15:29
0

Signed integers (we'll use 16 bit) range from -32768 to 32767 (0x8000 to 0x7FFF) while unsigned integers range from 0 to 65535 (0x0000 to 0xFFFF). So unsigned integers cannot have negative values, which is why your loop never terminates. However, you have told the print program to format the output as if it were signed (%d) so that you see the numbers formatted as negative values in your output. At some level, everything in a computer is just a string of bits that needs interpretation and your code example uses two different interpretations of the same bit patterns ... your unsigned int.

Steve Valliere
  • 1,119
  • 7
  • 12