[C Language] Tell you why unsigned char a=-1; %d is printed out as 255

1. Automatic type conversion process

 It should be noted that  when automatic type conversion is performed, the type of the original data does not change, but a converted intermediate variable is obtained

It's not that char is converted into int and then into long...

Into the title

Look at a piece of code and guess what the printed result is


   
   
    
    
  1. #include <stdio.h>
  2. int main()
  3. {
  4. char a = -1;
  5. signed char b = -1 ;
  6. unsigned char c = -1 ;
  7. printf( "a=%d,b=%d,c=%d", a, b, c);
  8. return 0;
  9. }

The result is: a=-1, b=-1, c=255

It can be considered that under the current compiler, char is equivalent to signed char

We know that in C language, one byte (8 bits) is allocated for a character, and an int integer is allocated 4 bytes (32 bits)

The integer data is stored in the complement form of the integer


- 1

Original code : 1000 0000 0000 0000 0000 0000 0000 0001

Inverse: 1111 1111 1111 1111 1111 1111 1111 1110

Complement code: 1111 1111 1111 1111 1111 1111 1111 1111


Assign -1 to char type variable a, truncate 8 bits from the back

a

Complement: 1111 1111

When printing with %d , automatic type conversion is performed, and the highest bit of a is considered to be the sign bit, the highest bit is 1, and 1 is added from the left to 32 bits

Print the complement of the intermediate variable of a as:

        1111 1111 1111 1111 1111 1111 1111 1111

        1000 0000 0000 0000 0000 0000 0000 0000

Original code: 1000 0000 0000 0000 0000 0000 0000 0001

So a prints -1, and b is the same .


c is an unsigned char type, when printing with %d , automatic type conversion is performed, the compiler thinks that the highest bit of c is not a sign bit , and 0 is added from the left to 32 bits

Thus printing the intermediate variable's complement of c is:

0000 0000 0000 0000 0000 0000 1111 1111

Converted to decimal is 255

So what c prints out is 255

To sum up, the type in front of the variable will not have any effect on the storage of the variable in the computer, such as char c = -1 and unsigned char c = -1, whether it is char or unsigned char type, -1 is pressed The reason is that four bytes are stored in complement code, but because it is one byte, the last byte is reserved. char and unsigned char only affect the compiler, affecting how the compiler treats this string of 0 The explanation with 1 will have an impact when comparing the size. The highest bit of unsigned char is also a value, and the highest bit of char is a symbol. When unsigned char c = -1 is compared with 3, it is first converted to the same type, both int, c will be expanded into int type, and then compare the size

1. Automatic type conversion process

 It should be noted that  when automatic type conversion is performed, the type of the original data does not change, but a converted intermediate variable is obtained

It's not that char is converted into int and then into long...

Into the title

Look at a piece of code and guess what the printed result is


   
   
  
  
  1. #include <stdio.h>
  2. int main()
  3. {
  4. char a = -1;
  5. signed char b = -1 ;
  6. unsigned char c = -1 ;
  7. printf( "a=%d,b=%d,c=%d", a, b, c);
  8. return 0;
  9. }

The result is: a=-1, b=-1, c=255

It can be considered that under the current compiler, char is equivalent to signed char

We know that in C language, one byte (8 bits) is allocated for a character, and an int integer is allocated 4 bytes (32 bits)

The integer data is stored in the complement form of the integer


- 1

Original code : 1000 0000 0000 0000 0000 0000 0000 0001

Inverse: 1111 1111 1111 1111 1111 1111 1111 1110

Complement code: 1111 1111 1111 1111 1111 1111 1111 1111


Assign -1 to char type variable a, truncate 8 bits from the back

a

Complement: 1111 1111

When printing with %d , automatic type conversion is performed, and the highest bit of a is considered to be the sign bit, the highest bit is 1, and 1 is added from the left to 32 bits

Print the complement of the intermediate variable of a as:

        1111 1111 1111 1111 1111 1111 1111 1111

        1000 0000 0000 0000 0000 0000 0000 0000

Original code: 1000 0000 0000 0000 0000 0000 0000 0001

So a prints -1, and b is the same .


c is an unsigned char type, when printing with %d , automatic type conversion is performed, the compiler thinks that the highest bit of c is not a sign bit , and 0 is added from the left to 32 bits

Thus printing the intermediate variable's complement of c is:

0000 0000 0000 0000 0000 0000 1111 1111

Converted to decimal is 255

So what c prints out is 255

To sum up, the type in front of the variable will not have any effect on the storage of the variable in the computer, such as char c = -1 and unsigned char c = -1, whether it is char or unsigned char type, -1 is pressed The reason is that four bytes are stored in complement code, but because it is one byte, the last byte is reserved. char and unsigned char only affect the compiler, affecting how the compiler treats this string of 0 The explanation with 1 will have an impact when comparing the size. The highest bit of unsigned char is also a value, and the highest bit of char is a symbol. When unsigned char c = -1 is compared with 3, it is first converted to the same type, both int, c will be expanded into int type, and then compare the size

Guess you like

Origin blog.csdn.net/m0_53573725/article/details/132504545