I would like to use this article to tell you some advanced knowledge of embedded C language, commonly known as "sao operation", to help you take your level to a higher level!

Edited from: https://mp.weixin.qq.com/s/42Fxs8rDf2rQ8YR8db74xw

Teacher Yu Zhennan had already started to participate in the international programming competition of the magnitude of ACM (ACM ICPC Northeast China Division) as early as 2005, and achieved very good results. It can be seen that when many talents are just getting started, their C language level is already relatively deep.

The following quotes from Mr. Yu Zhennan's "My University" series of online articles (excerpted from Zhihu):

ACM is really a contest of strength and strategy, which contains many principles of life. The most important thing I have learned is "you will win if you work hard"! !
We will still unite as before and work hard for ACM to go further. So far, we have held department competitions and school competitions. In the near future, we will let ACM go to the whole province, the whole country, and even the world. Of course, this also requires the joint efforts of several generations.

Online comments on Mr. Zhennan’s article "My University" (excerpted from Zhihu):

The host is from Hubei. He has graduated from university for 6 years. Looking back on his university, Guo Tianxiang and Yu Zhennan are the most influential people. It is the amazing experience and learning tutorials of these two people that made my university life a little easier. It has some direction and is not hazy. It is now 2018. Looking back on the scene in 2010, I feel great emotion and gratitude. I also hope that the latter can study hard in college and not waste his good time in college, although I didn't do it well myself.

We know that teachers Yu Zhennan and Guo Tianxiang have recorded many classic video courses for everyone, bringing many people to the field of single-chip microcomputer and embedded. But for many years, we have not seen the systematic C language courses recorded by Mr. Yu Zhennan.

I asked the teacher this question, and his answer was: " Although I have been using C language for 20 years, I have always thought that I am not qualified to teach C language courses. Until now, I have the courage to record this This set of courses "Climbing to the Top of Embedded C Language (Master C)" hopes to help everyone. Help everyone further improve their C language level to another level! "

The following is a chapter in Teacher Yu Zhennan's new book "Zhennan Technology Records--Ten Years Past", he explained to us 26 advanced knowledge and skills of C language. (Copyright has been authorized by Teacher Yu Zhennan)

Chapter 2 "Some "sexy operations" of C language and their deep understanding"

1. The essence of a string is a pointer

(How to convert 35 to the corresponding hexadecimal string "0X23"?)

2. Escape character\

(Type the "spy" inside the string.)

3. Connection of string constants

(String constants are double-sided tape, did you know?)

4. Splitting techniques for long strings

(The parsing of GPS data frames NMEA, Shell command line and AT commands is a typical application of long string splitting.)

5. The numbers of those who take the value by chance

(Must have to operate when playing multiple digital tubes.)

6. The essence and usage skills of printf

(Think you know printf very well? Have you ever tried printing to 3 UARTs? Or printing to an LCD screen?)

7. Regarding the transmission of floating point numbers

(Floating point is just an illusion, see its essence clearly.)

8. Direct manipulation of data

(How to quickly calculate the opposite of a floating point number, multiply by -1.0? Think again.)

Nine, rounding and comparison of floating point

(The teacher said that floating points cannot be directly evaluated. Why?)

10. The superb for loop

(Are you familiar with for loops? OK, Zhennan has asked a few questions, let’s try them.)

11. Hidden infinite loop

(We are in the light, sometimes the endless cycle is in the dark.)

12. The seemingly redundant empty loop

(Useless things?)

13. Independent executive body

(Have you never learned this concept in C language? That’s right, I use it often.)

Fourteen, multi-use () no harm

(Anything can be parenthesized.)

15. The reverse test of ==

(Writing == instead of = will allow you to adjust the program to vomit blood.)

16. The essence of assignment operation

(The C language assignment operation has puzzled mathematics professors for half his life.)

17. About complement code

(It's a showdown, and the CPU actually doesn't do subtraction.)

18. About -1

(-1 is all F, and all F is -1.)

Twenty, byte fast bit reverse order

(Mutual transformation of time and space – theory of relativity in computers)

21. About volatile

(Some things are not optimizable.)

22. About variable exchange

(Wonderful bit manipulation.)

23. About sizeof

(Tell you about the issues that few people pay attention to about sizeof.)

24. Efficiency of memcpy

(Small functions also have big backgrounds)

25. The essence of []

(Do you think [] is just an array subscript?)

Twenty-six, # and ## (serialization and connection)

(A knowledge point that has never appeared in C language textbooks)

C language is a very flexible and powerful programming language. For the same algorithm or function, we can write it in a fair way, or we can write it in an obscure way. Moreover, many people who claim to be programming masters just like to write programs in heavenly scriptures, thinking that they can realize correct functions even if others cannot understand them, which is a manifestation of superb technology. I don't comment on whether this approach is advisable or not, because everyone has their own style and personality. Let him program against his will, and programming may become boring and no fun. I just want to say that writing programs in a style requires capital and a deeper understanding of the C language. Many times it is not that we want to write programs that are difficult to understand, but that we want to understand other people's programs. In this chapter, Zhennan lists some programming techniques that I have seen and used, and provides an in-depth analysis.

If you are interested, you can register on the website to view the tutorial:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-B0IypvpF-1693141960058)(https://mmbiz.qpic.cn/sz_mmbiz_jpg/2ibmcljDoyWT5FJVUVQdKSJ81Mia1ds4jzSIpSnJekAibvib08GIoEdN8ZHr VeKQe7DzPWiaQJUcXAwVlDlyiagZs7EQ/640?wx_fmt=jpeg&wxfrom =5&wx_lazy=1&wx_co=1)]

1. The essence of a string is a pointer

String is the most basic concept in C language and the most commonly used. In embedded development, we often want to display some strings on the serial port assistant or debugging terminal through the serial port as a message prompt, so that we can understand the running status of the program; or convert the value of some constants into strings to Displayed on display devices such as LCDs.

So what exactly is a string in C language? In fact, the string itself is a pointer, and its value (that is, the address pointed by the pointer) is the address of the first character of the string.

To explain this problem, I often give an example: how to convert a numerical value into the corresponding hexadecimal string. For example, convert 100 to "0X64".

We can write a function like this:

void Value2String(unsigned char value,char *str)

{

unsigned char temp=0;

str[0]='0';str[1]='X';str[4]=0;

temp=value>>4;

if(temp>=0 && temp<=9) str[2]='0'+temp;

else if(temp>=10 && temp<=15) str[2]='A'+temp-10;

temp=value&0X0F;

if(temp>=0 && temp<=9) str[3]='0'+temp;

else if(temp>=10 && temp<=15) str[3]='A'+temp-10;

}

No problem, its functionality is correct. In terms of implementation, because the values ​​0 9 and A F are not continuous in ASCII code values ​​(0X30 0X39 and 0X41 0X46 respectively ), the program uses 9 as the boundary and performs case-by-case processing.

But smart programmers may use this method to achieve:

void Value2String(unsigned char value,char *str)

{

char Hex_Char_Table[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

str[0]='0';str[1]='X';str[4]=0;

str[2]=Hex_Char_Table[value>>4];

str[3]=Hex_Char_Table[value&0X0F];

}

Yes, this is the idea of ​​using a lookup table. Although 0 9 and A F are not continuous in ASCII code values, we can put them into an array to create a continuity. Then use the numerical value as a subscript to directly obtain the corresponding character.

Some people may think that the definition of Hex_Char_Table is too troublesome, and it is necessary to input characters one by one. In fact, you can do this:

void Value2String(unsigned char value,char *str)

{

char *Hex_Char_Table="0123456789ABCDEF";

str[0]='0';str[1]='X';str[4]=0;

str[2]=Hex_Char_Table[value>>4];

str[3]=Hex_Char_Table[value&0X0F];

}

We replaced the character array with a string constant. In fact, their expression in memory is almost the same, and their essence is a sequence of bytes in memory. As shown in Figure 2.1.

Figure 2.1 Character arrays and strings are both byte sequences in memory

The difference is that when defining a character array, you must clearly specify the size of the array, that is, how many characters (bytes) it can hold. The length of the string is based on the first byte equal to 0. Therefore, in the byte sequence of the string, there must be a byte with a value of 0, which is the terminator of the string. The strlen function we usually use to calculate the length of a string is actually detecting this 0. Therefore, if we pass a character array (byte sequence) without 0 to strlen, the final result is likely to be wrong, and the program may even crash due to out-of-bounds access to the array.

Above, Zhennan said that "the string itself is a pointer", so the moment to witness the true meaning of this sentence has come, and we will continue to simplify the above program:

void Value2String(unsigned char value,char *str)

{

str[0]='0';str[1]='X';str[4]=0;

str[2]="0123456789ABCDEF"[value>>4];

str[3]="0123456789ABCDEF"[value&0X0F];

}

The pointer variable Hex_Char_Table is actually redundant, "the string itself is a pointer", so you can directly use [] with the subscript to extract the characters in it. Any variable or constant that is essentially a pointer type (that is, expresses the address meaning) can directly use [] or * to access the data elements in the data sequence it points to.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-DdGdEKH2-1693141960059) (https://mmbiz.qpic.cn/mmbiz_jpg/2ibmcljDoyWQfE7zRyFjibgmyqffdVPByhWwUz7zr8ibH2bcUQWzGwIJ3IHL vmxtWzYEiaahqzVpxCVdazYHmQcpjA/640?wx_fmt=jpeg&wxfrom =5&wx_lazy=1&wx_co=1)]

Guess you like

Origin blog.csdn.net/qq_41854911/article/details/132527909