unsigned integers in C [closed]
Asked Answered
A

9

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);
   }
Aegina answered 11/3, 2013 at 13:55 Comment(3)
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.Aulic
@stupid_idiot, also it affects comparison, multiplication (mul/imul), division (div/idiv) and propagation to larger types. Maybe something else, not quite sure.Hollow
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".Bequeath
S
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.

Suicide answered 11/3, 2013 at 13:59 Comment(2)
I think you mean non-negative. 0U is definitely not positive. :-)Lucubration
@R.. Indeed. I'll edit, duh :-)Suicide
S
2

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

Steeple answered 11/3, 2013 at 13:57 Comment(0)
H
2

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.

Hollow answered 11/3, 2013 at 13:58 Comment(1)
yes here is this is the program and running on linux #include<stdio.h> int main() { unsigned int i; for (i = 10; i >= 0; i--) printf ("%d", i); return 0; }Aegina
V
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.

Valdivia answered 11/3, 2013 at 14:0 Comment(2)
@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.Malchy
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.Lexie
D
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.

Departure answered 11/3, 2013 at 14:0 Comment(0)
A
0

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

Amygdalin answered 11/3, 2013 at 13:59 Comment(0)
F
0

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

Ferriferous answered 11/3, 2013 at 13:59 Comment(1)
@sixlettervariables haha sorry I meant %uFerriferous
M
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.

Mauriciomaurie answered 11/3, 2013 at 14:0 Comment(2)
when using '%u' in outputs like 10987654321042949672954294967294429496729342949672924294967291429496729... what does it it mean.i tried only to print from 10 to 0 unsigned numbersAegina
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)Mauriciomaurie
U
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.

Utter answered 11/3, 2013 at 14:1 Comment(2)
Don't make assumptions about the bit width of unsigned and int.Valdivia
No assumption on my part, that's why I said "we'll use 16 bit" as an exampleUtter

© 2022 - 2024 — McMap. All rights reserved.