当前位置:网站首页>C secret script Chapter 1: data storage (in-depth analysis) supplement

C secret script Chapter 1: data storage (in-depth analysis) supplement

2022-06-12 14:20:00 Mortal programming biography

Last time we ended with a question , Now it is up to me to explain for you . I don't say much nonsense , Go straight to the question .

#include<stdio.h>
int main()
{
unsigned int i;
for(i=0;i>=0;i--)
{
printf("%u\n",i);
}
return 0;
}

What is the output result ?

analysis : First, we create an unsigned integer variable i, Here we only need to think of this when we see unsigned types The value of a variable cannot be negative , Then we wrote a for Cycle if >=0, Every loop variable i reduce 1, Finally, print in unsigned form . So we mentioned that earlier , Unsigned numbers cannot be negative , So it can be seen that ,i>=0 Hang up , The output result of the program is dead loop .

Let's look at the next question

#include<stdio.h>
#include<string.h>
int main()
{
char a[1000];
int i;
for(i=0;i<1000;i++)
{
a[i]=-1-i;
}
printf("%d",strlen(a));
return 0;
}

What is his output ?

analysis : This problem is still a little difficult , First we created a 1000 A one-dimensional character array of elements and a int Variable of type i,for The cycle condition is i<1000 And every time the loop is executed, the a[i] Array traversal 1-i, Final chain access strlen Function print . This can be explained by strlen function ‘\0’ Stop calculating the length , But this character array does not store strings , ha-ha , I know it must be a random value . You are wrong to think so, Taoist friend , At first, I thought so too . The right way of thinking : We know char The value range of type is :-128~127, Then we 1000 Secondary cycle -1-i Can you store such a large number ? Obviously not , When we a Value assigned to -127 Again -1 when , Because you are stored in char Of type ,char Type will convert the number beyond the value range to the number within the range , therefore -128-1 It becomes 127. Let's take a look at the end position we want to find \0, Know that a The value of is 0 Time is \0, So that is -128~127 The number of does not include 0, It adds up to 255.

Next question :

#include<stdio.h>
int main()
{
unsigned char i=0;
for(i=0;i<=255;i++)
{
printf("hello world\n");
}
return 0;
}

analysis : I believe this problem should not be difficult , It's still the old way , When you see the unsigned character type, you first think of the value range 0~255 And cannot be negative , So we know for loop i<-255 Hang up , Form a dead cycle .

Well, let's move on to the highlight of this chapter , It's also the hardest part .

How floating point numbers are stored in memory :

according to IEEE754 standard , Any binary floating point number v It can be expressed in the following form .

  1. (-1)^ s*M*2^E
  2. (-1)^  The sign bit , When s=0,v Is a positive number ; When v=-1,v It's a negative number .
  3. M Represents a significant number , Greater than or equal to 1, Less than 2.
  4. 2^E Indicates the index bit

for example :9.0 The storage form in memory is as follows :(-1)^0*1.001 * 2 ^3

Floating point numbers are stored in memory as binary numbers :5.0 His integer part is 101 His decimal part is 0 So that is 101.0 ; also 0,5 The binary of is 0,1 This is not decimal 0,1, The first digit to the right of the decimal point is -1 Power 1 Of -1 The second fraction is one-half 0-5 

 IEEE754 standard : about 32 Floating point number of bits , The highest 1 Bits are sign bits s, And then 8 Bits are exponents E, The rest 23 Bits are significant numbers M( This is the standard for single precision floating point numbers ) And the index E8 Bits are unsigned numbers . But we know that , In the law of science and technology E You can have negative numbers , therefore IEEE754 Regulations , In memory E The true value of must be added with an intermediate number , about 8 Bit E, The middle number is 127; about 11 Bit E, The middle number is 1023. for example :2^10 Of E yes 10, So save as 32 When floating-point numbers are in place , Must be saved as 10+127=137, namely 10001001.

E There are three situations when you can take it out of memory

1.E Not all for 0 Or not all of them 1

Such as 5.5 The binary form of is 101.1  754 The standard for the :(-1)^ 0 * 1.011 * 2^2 and E The real value of is 2+127 That is to say :  0  10000001 01100000000000000000000

2.E All for 0

At this time , The exponent of a floating point number e Rule of death 1-127( Single-precision floating-point ) or 1-1023( Double precision floating point ) For real value , Significant figures m No more first 1, It's reduced to 0.xxxxxxxx Decimals of .

3.e All for 1

At this time , If the significant number m All for 0, Infinity . because e+?=1111111, therefore e The true value of is 128

Well, this chapter is over , All Taoist friends and seniors praise me , If there is any deficiency, please give me some advice , I'm leaving .

原网站

版权声明
本文为[Mortal programming biography]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010512215341.html