Today, a small program, I feel that some of the problems are easy to overlook!
This program code is as follows:
The result of the program is:
I think a lot of small white like me may only start to wonder why the final result is 255! First, we need to know that strlen () is a function that calculates the length of a string, but why is the last string length 255? The defined array A does not have 1000 elements, and the For loop is executed 999 times?
For Char, we have to know that the implied end tag is \ n, and when the compiler recognizes a variable of type char, it is the end of the read; For this program, we also have to pay attention to the value of char circumference: -128~127
So when i = 128 a[128] =-1-128 =-129, then there is a problem, the minimum value of the element in a can only be-128, how to do? Here you have to know that in the computer, the storage data are in a complement of the storage, 128 in the computer encoding is 1000 0000, 1 of the complement is 1111 1111, so 129 of the complement is 1 0111 1111, but char in the bits only eight bits, So put the highest bit away, the rest is as a complement, that is 0111 1111 (0x7f) through the cycle, in turn minus one 0x7d,0x7c,0x7b,0x7a,0x79,0x78,0x77,0x76,0x75,0x74,0x73,0x72,0x71 , 0x70 ... 0x00, so when encoded as 0x00, a[255]= 0; So when a[255]= 0 o'clock, as the flag for the end of the string, the element in a[0]~a[254] is not 0, so the result of the final program execution is that the length of string A is 255.
Understanding of char types and understanding of the complement