Unsigned int doesn’t work?

Asked

Viewed 557 times

5

Next, my program keeps compiling even after I have assigned a negative value to an unsigned variable, why does this happen? I use IDE Code::Blocks 13.12

unsigned int numero1 = -1;
  • What happens?

  • The program runs normal. Case I of a printf("%i", numero1); show me -1.

3 answers

6

Attribution converts -1, which is a int, for unsigned int.
The rule of conversion of Signed-to-unsigned is (INT_MAX + 1) - intValue, in this case as intValue is -1 the result is INT_MAX

If you use printf("%i", numero1) will be presented -1.
To display the correct value use printf("%u", numero1)

Source: Soen

  • with printf("%u", numero1"); it is displayed 4294967291, however, if I take the unsigned it continues displaying that number...

  • Yes, that’s to be expected, because %u converts numero1 for unsigned, just like, when you used %i, he was converted to int.

  • Then the unsigned is useless?

  • It is useful if you need its full range of positive value (0 to 4,294,967,295 instead of -2,147,483,648 to 2,147,483,647) or when used in operations bitwise.

3

Two things are happening:

  • first implicit conversions between int and unsigned int (if you activate all compiler warnings, it is likely that it warns you that the code is kind of "weird");

  • second, since the printf function accepts any type, you need to specify manually how you want to display the value, with %d (which is equal to %i in the printf, but not in the scanf) or %u (again, if you activate all compiler warnings it may be that it warns about the incongruity between the declaration and the printf, depending on the compiler version)

Is unsigned useless? Well, it has its purpose, but it is true that for error checking it does not even serve!

For example, if you do

unsigned int numero1 = 1 - 2; // Dá -1, mas vai ser convertido para unsigned
if(numero1 < 0){ // nunca será true
    // ...
}

you will have a beautiful and useless if that will never perform because the condition is always false (the result of the operation is being interpreted as a very large positive number), but if you take the unsigned if will work (the same pattern of bits inside the computer will be interpreted as -1).

-1

Probably the compiler is removing the sign and assigning only the value 1the variable, make a printf of this variable to test. when print with %u will be disregarded the bit that represents that the value is negative/positive and will consider it part of the digit will soon generate a gigantic number 4294967291 when printing with %i` you will consider the last digit as the representative of the number type (negative/positive).

  • The program runs normal. Case I of a printf("%i", numero1); show me -1

  • you must use %u.

  • with printf("%u", numero1"); it is displayed 4294967291, however, if I take the unsigned it continues displaying that number..

  • The compiler does not "remove the signal" literally, but generates code that interprets the bit as magnitude instead of signal (you also said that, but it got a little confused). This affects comparisons, multiplications, etc. again about the type of variable, so it is this type that ends up prevailing (but you should always pass the right type, because not all types have the same size).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.