Weird strings in C language

Let's look at an example.

int main() {
    int i = 65;
    printf((const char *)&i);
    system("pause");
    return 0;
}

You will be surprised to find that the letter A is printed on it, and then look at an example

int main(){
    int i = 4407873;
    printf((const char *)&i);
    system("puase");
    return 0;
}

You will find that ABC is printed, so how do these strange strings of numbers come from?
First of all, you need to know what is little-endian storage, and int type is little-endian storage, and what is little-endian storage will not be introduced. , readers can Baidu, or browse the blog.
Let's take the example of 65 to illustrate. We will use 65 as an int type, occupying 4 bytes, and a byte occupying 8 bits. Here, the binary number of 65 converted into 32 bits is 00000000000000000000000001000001, and the int type is small End storage, so long can be seen as 1000001000000000000000000000000, and then combined with the printf function to explain, the printf function reads from the left, one byte at a time, so the first read is 10000010, and then read to 00000000, this is not Is it just the binary number of '\0'? So the read is A. Similarly, you convert 4407873 into binary, store it in little endian, and then read it again. You will find that the read is still ABC'\0', and the end of reading is 0.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569320&siteId=291194637