C language programming suggestions and techniques are also applicable to the learning of microcontrollers.

Publisher:chwwdchLatest update time:2022-12-15 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  • Rule 6: There is no Rule 6.

  • data programming

    Unlike many if statements, algorithms or the details of an algorithm are usually encoded in compact, efficient, and unambiguous data. The work at hand can be codified, ultimately because its complexity is composed of unrelated details. Parse tables are a classic example, encoding the syntax of a programming language in a form that parses fixed, simple code segments. Finite state machines are particularly well-suited to this form of processing, but almost any program useful for building data-driven algorithms involves "parsing" inputs of some abstract data type into sequences of independent "actions."

    Perhaps the most interesting thing about this design is that the table structure can sometimes be generated by another program (the classic case is the parsing generator). As a more down-to-earth example, if the operating system is driven by a set of tables that contain operations for connecting I/O requests to the corresponding device drivers, then the system can be "configured" through a program that can read a certain Description of the connection between some special equipment and the suspicious machine, and prints the corresponding table.

    One of the reasons why data-driven programs are less common among beginners is due to Pascal's tyranny. Pascal, like its founder, believed in the separation of code and data. Thus (at least in raw form) initialized data cannot be created. It runs counter to the theories of Turing and von Neumann, which are fundamental principles that define storage computers. Code and data are the same, or at least sort of. How else to explain how a compiler works? (Functional languages ​​have similar problems with I/O).

    function pointer

    Another consequence of Pascal's tyranny is that beginners don't use function pointers. (There are no functions as variables in Pascal.) There is something interesting about using function pointers to handle coding complexity.

    The program pointed to by the pointer has a certain complexity. These programs must adhere to some standard protocols, such as requiring a group of programs to all call the same. Beyond that, all that is required is to complete the business, and the complexity is dispersed.

    There is a protocol that states that since all functions used are similar, they must also behave similarly. This is helpful for simple documentation, testing, program extensions and even for distributing programs over a network - remote procedure calls can be encoded through this protocol.

    I think the core of object-oriented programming is the clear use of function pointers. Specify a series of operations to be performed on the data, and a complete set of data types that respond to these operations. The simplest way to hold a program together is to use a set of function pointers for each type. In short, it is to define classes and methods. Of course, object-oriented languages ​​offer more and fancier syntax, derived types, and so on, but conceptually offer little extra.

    The combination of data drivers and function pointers results in a surprisingly performant way of working. In my experience, this approach often yields surprising results. Even without an object-oriented language, 90% of the benefits can be obtained without additional work and the results can be better managed. I can't recommend a higher standard of implementation. All of my programs are organized this way and have survived multiple developments without problems - far better than the less constrained approach. Perhaps as the saying goes: discipline pays off handsomely in the long run.

    File contains

    Simple rule: You should never nest includes when including files. If a declaration (in a comment or an implicit declaration) requires files that are not included first, it is up to the user (programmer) to decide which files to include, but in a simple way and in a structure that avoids multiple inclusions. Multiple inclusions are the bane of systems programming. It is not uncommon to compile a single C source file by including the file five or more times. /usr/include/sys in Unix systems uses such a terrible method.

    Speaking of #ifdef, there is a small episode. Although it can prevent the file from being read twice, it is often used incorrectly. #ifdef is defined in the file itself, not the file containing it. The result is often thousands of unnecessary lines of code passing through the lexical analyzer, which is the most expensive stage (in a good compiler).

    Just follow the above simple rules to make your code elegant and beautiful, or at least pleasing to the eye, turning technology into art~~

    Further reading

    Some people think that now is the era of java and .net, who still needs C and assembly? Who knows, Java and .net are built on software, and they are systems established to monopolize the market. It is like digging a pit with a golden wall and asking you to jump down. You still think you are standing on the shoulders of giants. , in fact, became the frog in the pit. To become a real programmer, and to become a master programmer, you must start from the machine, from CPU to operating system, and then to the software system. The realm of a master is the mirror after enlightenment. If the software design is superb, I am the program. The program is me.

    Onlooker Li Si said: This person is stupid! I use the mouse to drag a few controls, and it becomes a xxx management system. You might not be able to write it in C language for a year! Well, I have to admit that those who say this are already slaves of mS. I don’t know anything else. MFC itself is a closed architecture. If you start learning from MFC, you will only form a closed thinking mode, because MS hopes that many people will only learn superficial things and not become masters, so it strongly recommends so-called visual program development tools. There are really many people who are willing to be fooled by it and finally lose their way. Let's say he can't handle the program. He can do it, but if the program is more complicated and a problem occurs, he won't be able to figure out where the problem is. It's unclear anyway!

    Liang Zhaoxin, a great guy, said: "I just don't understand, how to write a program with a mouse? In my company, the keyboard of experts keeps beeping, the mouse beeps occasionally, and the novice's mouse keeps beeping. , the keyboard rings occasionally, the difference in their salaries is not that much!"

    C language is the basis of all major operating systems. The kernels of Unix, Linux, and Windows are all developed in C language (in some places, they are developed mixed with assembly language). Didn’t you see that Windows API is an interface for C language functions? The vast majority of Unix/Linux applications are developed in C language; there are not many Windows applications developed with pure API, and most of them rely on some kind of Application Framework, such as the so-called VC++, which actually refers to VCIDE+C++ language+MFC (now the focus has shifted to ATL, WTL), but underlying software such as Windows services, networks, and drivers are still developed in C language. Compilers for various languages, including the Java virtual machine, are developed in C language. Various embedded devices, such as mobile phones and PDAs, are also developed in C language.

    Here are some personal suggestions:

    • Read more textbooks and codes

    Because C language is flexible and powerful, it is very difficult for beginners to fully master it. Therefore, in the process of learning C language, you should read more textbooks and codes. If you don't find it in the textbook, you can search online. First of all, you must be proficient in variables, constants, basic data types, library functions and their characteristics and applications, operators, expressions and statements, and the basic format written in C language. Once again, you need to master basic knowledge such as flow control statements, arrays, functions, and pointers in C language. Once you are proficient in the above knowledge, you can learn knowledge such as linked lists, queues, trees, and graphs. Finally, you must be proficient in the application of each knowledge point, and you can focus your learning on key issues such as the function design framework, parameter design, and return value design.

    • Learn math and English well

    In the process of learning C language, there are generally a large number of algorithms and data structures that need to be understood (freshmen students will be exposed to this knowledge in their sophomore year. If there are students who want to know in advance, you can click the link below to view:), many arithmetic operations Logical operations, relational operations, and loop structures can all be completed using mathematical knowledge. Likewise, many algorithms are designed to complete calculations in the field of mathematics. The purpose of writing programs is to allow computers to operate the computing process instead of humans, thereby reducing manpower. It can be seen that mathematics plays an important role in computer learning. With mathematical knowledge, you will find that data structures and algorithms are also very simple. In the same way, in the process of learning C language, we will use a lot of English knowledge. For programming, the role of English is reflected in reading English documents and adapting to the international programming environment. We must remember some commonly used vocabulary in C language, that is, many keywords.

    • Link theory with practice and pay attention to computer testing

    Most courses in the computer major test learning results through practice. More importantly, the theoretical knowledge learned must be put into better use in practice. Programming is a practical job, and it is impossible to just talk without practicing. When you first start learning, you can practice more exercises in the book. For things you don't understand, the best way is to write a small program and experiment with it, which can leave a deep impression on yourself. In the process of doing it yourself, you must constantly correct your bad programming habits and misunderstandings. C language is also a very practical course. It requires not only mastering the concepts, but also hands-on programming, debugging and running on the computer. Develop a good habit of analyzing the topic before going on the computer and compiling the program source code. When programming, pay attention to the program format, punctuation, etc. At the same time, be patient when debugging the program. Sometimes a program may need to be modified multiple times, which may even cost a lot of money. After a lot of effort, there was still no result. You must constantly ask teachers or classmates for advice and consult information. Therefore, you must not give up when programming is in trouble. This time is the key to improving your level. You must persist to the end. Everyone must have confidence in themselves and in learning the C language course well, so that we can have a good learning state and correct bugs. After the program is successfully debugged, you should summarize and analyze the shortcomings you have experienced when writing the program, and the problems you should pay attention to in the future problem-solving process. After successfully debugging the program on the computer, you must complete the experimental report and gradually accumulate experience in debugging the program. Develop yourself good programming habits.

    [1] [2] [3] [4]
    Reference address:C language programming suggestions and techniques are also applicable to the learning of microcontrollers.

    Previous article:I have a microcontroller, do you have wine?
    Next article:Summary of commonly used algorithms for sensor data in microcontroller development

    Latest Microcontroller Articles
    Change More Related Popular Components

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews


    Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

    Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号