Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language problem main () {int I =-1; Unsigned u = 65536printf ("%d, %u ",i, i); printf("%d,%u ",u,u); } want to answer the reason
C language problem main () {int I =-1; Unsigned u = 65536printf ("%d, %u ",i, i); printf("%d,%u ",u,u); } want to answer the reason
The output result is:

- 1,4294967295

65536,65536

The analysis is as follows:

% d-integer print format, signed.

%u— Integer print format, unsigned.

I think the output results of the other three should be very clear, mainly why is the second one 4294967295?

The value range of unsigned int is 0~4294967295, and the integer value is stored in memory as its complement, which is positive.

The complement itself is, but the complement of a negative number is not. Let me explain it with a memory size of one byte:-1.

Original code: 1, 000? 000 1, the highest bit indicates the sign bit, 0 is positive and 1 is negative. The conversion method of its complement is: the sign bit is unchanged and the value is unchanged.

Add 1 to the bit inversion, so the complement of-1 is: 1,1? 1 1 1 1。 For 32-bit 4-byte int, the conversion method is the same.

So, int? I=- 1, and the complement values stored in the memory are: 1,11? 1 1 1 1? ...? 1 1 1 1? 1 1 1 1。 When output in %u format, the nature of the most significant bit 1 will change, and the compiling system will change the most significant bit 1.

Also as digital bits instead of sign bits, the output value is 4294967295.