Unsigned values ​​in C

I have the following code:

#include <stdio.h> int main() { unsigned int a = -1; int b = -1; printf("%x\n", a); printf("%x\n", b); printf("%d\n", a); printf("%d\n", b); printf("%u\n", a); printf("%u\n", b); return 0; } 

Output:

 ffffffff ffffffff -1 -1 4294967295 4294967295 

I see that the value is interpreted as signed or unsigned according to the value passed to the printf function. In both cases, the bytes are the same ( ffffffff ). Then, what is the word unsigned for?

+8
c printf unsigned
source share
4 answers

Set int -1 to unsigned : As -1 does not fit in the range [0...UINT_MAX] , and the multiplicity UINT_MAX+1 added until the response is in the range. Obviously, UINT_MAX on the machine is OP pow(2,32)-1 or 429496725 , so a has the value 4294967295 .

  unsigned int a = -1; 

The qualifier "%x" , "%u" expects an unsigned match. Because they do not match, β€œif the conversion specification is invalid, the behavior is undefined. If any argument does not fit the corresponding conversion specification, the behavior is undefined .” C11 Β§7.21.6.1 9. The printf specifier does not change b .

  printf("%x\n", b); // UB printf("%u\n", b); // UB 

The specifier "%d" expects an int match. Since they do not match, more than UB .

  printf("%d\n", a); // UB 

Given undefined behavior, conclusions are not supported.


both cases, the bytes are the same (ffffffff).

Even with the same bit pattern, different types can have different values. ffffffff as unsigned has a value of 4294967295. As an int , depending on integer encoding, it has a value of -1, -2147483647 or TBD. As a float it can be NAN .

What does an unsigned word mean?

unsigned stores an integer in the range [0 ... UINT_MAX] . It never has a negative meaning. If the code needs a non-negative number, use unsigned . If the code needs a counting number, which can be +, -, or 0, use int .


Update: to avoid compiler warning about assignment of signed int to unsigned , use below. This is a negation of unsigned 1u , which is well defined as stated above. The effect is the same as -1 , but conveys the direct intent of the compiler.

 unsigned int a = -1u; 
+5
source share

Having unsigned in a variable declaration is more useful for programmers themselves - do not treat variables as negative. As you noticed, both -1 and 4294967295 have an exact one-dimensional representation for a 4-byte integer. All about how you want to relate to or , see .

unsigned int a = -1; operator unsigned int a = -1; converts -1 to two additions and assigns the bit representation to a . The printf() x , d and u shows how the bit representation stored in variable a looks in a different format.

0
source share

When you initialize unsigned int a to -1; , this means that you keep the addition 2's -1 in memory a .
This is none other than 0xffffffff or 4294967295 .

Therefore, when you print it using the format specifier %x or %u , you get this output.

By specifying the variable signatures to decide on the minimum and maximum limit of the value that can be stored.

Like unsigned int : range from 0 to 4,294,967,295 and int : range from -2,147,483,648 to 2,147,483,647

For more information on signing, see this

0
source share

In hexadecimal, it cannot get a negative value. Therefore, he shows it as ffffffff .

The advantage of using an unsigned version (when you know that the values ​​contained in it will be non-negative) is that sometimes the computer detects errors for you (the program will be " crashed " when <a strong negative value is assigned to the variable).

-5
source share

All Articles