Scope of basic types of C Language

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <string. h>
 
Int main ()
{
Char arr [1000];
Int I;
For (I = 0; I <1000; I ++)
{
Arr [I] =-1-i;
}
Printf ("% d \ n", strlen (arr ));
 
Return 0;
}

What are the answers to this question? There must be different answers. The correct answer should be 255.

255? Some may wonder why it is 255, because the array type char represents a signed char.

 

In a for loop, when the I value is 0, the value of a [0] is-1. The key is-1 how to store in memory. We know that in a computer system, all values are represented (stored) by supplementary codes ). The main reason is that the complement code can be used to process the symbol bit and other digits in a unified manner. At the same time, the subtraction can also be processed by addition. In addition, when two numbers are added, if the highest bit (symbol bit) has an incoming bit, the carry is discarded. The positive complement is the same as the original one. The negative complement is 1, and the rest is the original one in the absolute value of the number.


According to the rules of negative complement, we can know that the-1 complement code is 0xff, and the-2 complement code is 0xfe ...... When the value of I is 127, the value of a [127] is-128, and-128 is the smallest negative number that can be expressed by char data. When I continues to increase, the value of a [128] cannot be-129. Because of this overflow,-129 requires 9 bits to be stored, and char data only has 8 bits, so the highest bit is discarded. The remaining 8 bits are the low 8 bits of the original 9-bit complement code, that is, 0x7f. When I continues to increase to 255, the low 8-bit value of-256 is 0. Then, when I is increased to 256, the low 8 bits of the-257 complement are all 1, that is, the low 8 bits complement is 0xff, so that a new round of loop is started ......


According to the above analysis, the values from a [0] to a [254] are not 0, while those from a [255] are 0. The strlen function calculates the string length and does not contain the '\ 0' at the end of the string '. The flag for judging whether a string ends is to check whether '\ 0' is encountered '. If '\ 0' is encountered, the string ends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.