Value range of char type

First of all, we need to know what determines the size of data: it is the memory space owned by its data type.

When we know its data type such as:

int occupies 4byte 

char accounts for 1byte

So the memory space they have (1byte = 8bit), that is, their binary bits, are:

int 32bit(32位):int    :1111 1111 1111 1111 1111 1111 1111 1111(4,294,967,168)

char 8bit(8位):char :1111 1111  (255)

The minimum value is 0

That is, when the sign bit is not considered :

int: 0 ~ 4,294,967,168

char : 0~ 255

When we consider the sign bit (the first bit of the binary system), its maximum and minimum values ​​change:

All possibilities of the value of char:

0000 0000

0000 0001

.....

0111 1110

0111 1111 (this is already the largest positive number of char type, 127)

Add 1 to get

1000 0000 (-128) This is a fixed value. When this number appears in char, it will be directly regarded as -128

1000 0001(-127)

What is stored here is the complement code. First convert it into the original code 1111 1110, 1111 1111 (-127)

Note: Here we need to distinguish the difference from printf printing (upgrade first and then see if you want to change the blog)

.....

1111 1110

1111 1111 is converted into the original code through complement code 1000 0001, which is -1

So from the above we can conclude that

The value range of signed char type:  -128 ~ 127

Attached:

How to determine the value of an unsigned char by giving you a signed char (for negative numbers, not necessary for positive numbers):

like:

When you are given -128, what does it take to convert it into a positive number: 

Then you can pass: 256 + this number 

256 + (-128) = 128

Same reason 

- 92: Now it is 256 - 92 = 168

On the other hand, if you want to find a negative number through a positive number, use 256 - positive number: 256 - 168 = 92

And you can also remember the range of char type through the following picture:

77e38c248bbf4736860d8e7c5dbb0ddc.png

The super-detailed note summary of C language (also including mind maps) has been updated, please pay attention early to avoid getting lost!

Guess you like

Origin blog.csdn.net/ZYK069/article/details/128628837