How should beginners learn C/C++ language well?

Hello friends, my name is Martin.

Learning the C/C++ language does not happen overnight, but it does not take ten years to become proficient.

Today, let’s talk about how to learn and become proficient in C language at the minimum cost. This is the theme of this article.

By the way, I would also like to share a C++ study notes warehouse called CppGuide "C/C++ Learning + Interview Guide" which covers the knowledge that most C++ programmers need to master. For entry, advanced, in-depth, school recruitment, social recruitment, and preparation for C++ learning & interviews, CppGuide is your first choice!

picture

Warehouse address: https://github.com/GrindGold/CppGuide

Recently, Mr. Li Wei talked about the video of the book "C++ Primer 5th". He is a senior engineer at Microsoft in the United States and has a doctorate from Tsinghua University. He will help promote it. If you are interested, you can watch it.

Please note: Even the "minimum cost" is by no means a shortcut. It is about achieving the most results in the shortest time. It also means that you need to go through a difficult process.

1. You must read good books, otherwise it is better not to read.

The first question that all beginners face is: how to choose teaching materials. A good start is half the battle, and choosing an excellent textbook is the key to getting twice the result with half the effort. Unfortunately, schools usually assign you a terrible C textbook; fortunately, you can choose again.

The famous Professor Tan Haoqiang has published a book "C Language Programming", which is said to have a circulation of more than 4 million. As far as I know, many schools will recommend this book as a C language textbook.

This book is not written for computer major students, but for students of other majors who need to take the computer level exam. The main shortcomings of this book are: the example programs are very unprofessional and cannot teach you the way of thinking that you should master in programming; the programming style is quite bad and will make you develop the bad habit of writing random codes; there are too many mistakes, and someone once I pointed out hundreds of errors in this book, many of which were critical conceptual errors. Okay, I don’t want to say too much about this book. If you are interested, you can Baidu it.

"The C Programming Language" (Chinese translation: "C Programming Language") by Kernighan and Ritchie can be called a classic among classics. However, many contents in the old version are outdated and far from the current standard C language. Everyone must read it. The latest version, otherwise it is better not to watch it.

In addition, even the most classic and authoritative book cannot cover everything, so it is necessary to keep a "C Language Reference Manual" on hand.

"C Reference Manual" is the "C Reference Manual", which is a detailed description of the C language standard, including details of most C standard library functions. It can be regarded as the best standard C language reference book.

By the way, the latest "C Programming Language" is revised according to the C89 standard, while the "C Language Reference Manual" describes the C99 standard. There may be some differences between the two. It is recommended to study according to the C99 standard.

There is also a book "C and Pointers", which is also quite well written. The English name is "Pointers on C". It particularly emphasizes the importance of pointers, which is a feature of this book.

However, this book is not very suitable for beginners. If you have ever learned C language and have some basic knowledge of C language but are not very solid, then you can try this book. I believe that as long as you understand pointers, C language will no longer be mysterious.

If you have finished reading a C language textbook and want to go further, then there are two books you must read. The first is "C Traps and Pitfalls" (Chinese translation: "C Traps and Pitfalls"), a very thin booklet with very interesting content.

It should be noted that this book was written more than twenty years ago, and many of the shortcomings of the C language mentioned in it have been improved, but it is not a bad thing to understand some history.

Then you can challenge "Expert C Programming" (Chinese translation: "Expert C Programming"). As the name of the book suggests, this book is quite difficult. Once you read it carefully and understand it thoroughly, you can confidently write on your resume "Proficient in C language".

Remember one principle: don’t read books that you don’t understand yet. It’s a waste of life. If you can't understand it, then you must be missing some necessary basic knowledge.

At this point, you need to carefully analyze what content you need to add, and then go to the bookstore to find books about these contents. Only by completing the basic knowledge and then coming back to learn will you truly get twice the result with half the effort.

2. Unix/Linux or Windows? This is a big problem

Different programming environments will create programmers with different thinking. Most Windows programmers rely on integrated development environments, such as Visual Studio, while Unix programmers prefer Makefiles and consoles.

Obviously, the integrated development environment is easier to use. To learn C language on Windows, you only need to press a few basic Visual C++ toolbar buttons to start writing Hello, World!, while under Unix, you need some console operations. basic knowledge.

Some people may think that the Unix environment is simpler, but the power of habit is great. Everyone is familiar with the basic operations of Windows, but it seems a bit uneconomical to install a Unix system specifically to learn C language.

For a novice who only knows the basic operations of Windows and doesn't even know anything about DOS, the most important thing is to do something interesting and meaningful as soon as possible.

Writing a small program in C language is far more interesting than learning ls, cat and other commands. Moreover, if we want to focus on the C language itself, we have to temporarily ignore some things, such as the compilation and linking process, the writing method of Makefile, etc.

Therefore, I suggest that beginners should use Visual C++ 6.0 (not VisualC++ .NET) or Dev C++ as the main learning environment, and do not get too entangled in IDE usage skills, because you must switch to the Unix environment in the future.

Visual C++ 6.0 is easy to use and intuitive to debug, but its default compiler does not support the C standard well, while Dev C++ uses the gcc compiler, which supports the C99 standard well.

By the way, many university C language courses still use Turbo C 2.0 as the experimental environment. This is quite undesirable. The first reason is that TC 2.0 has almost no support for the C standard. The second reason is that the programs compiled by TC 2.0 It is 16-bit, which will cause great trouble in understanding 32-bit programs in the future (of course, using things like djgpp can make TC 2.0 compile 32-bit programs, but that is too complicated).

After you finish studying a C language textbook, you must turn to the Unix platform to continue learning. Almost all advanced C language tutorials are based on the Unix platform (such as "C Expert Programming").

The transformation process is painful. You need to face a variety of complicated commands, which are completely different from the way of thinking on the Windows platform, but the pain is worth it.

Unix and C are symbiotic, and the way of thinking and habits of Unix are more in line with the way of thinking and habits of C language. Under Unix, you can find countless excellent source codes for you to read. You can easily view the online manual of a certain library function, and you can also see the best coding style (when it comes to coding style, I will write a dedicated (described in detail in this article).

It can be summed up in one sentence: If you are new to learning C language, it is recommended to use Windows system and integrated development environment. When preparing to work hard to become a "master", please turn to the Unix platform first.

3. Everything is ready, you are the east wind

The book has been selected, the environment configuration has been completed, and as the saying goes, everything is ready, all that is left is your own efforts. Please start from the preface of the book and read the textbook at hand carefully. Many people like to start reading directly from the first chapter. This is a wrong approach.

The preface is the author's general introduction to the entire book. The author will generally tell you what foundation you need to read the book smoothly, which can help you check whether you already have the basic knowledge. After reading the preface, you should also browse the table of contents to understand the overall structure of the book, and arrange a study plan for yourself.

When learning C language, you must pay attention to every detail. You must type the example codes in the book yourself. Only when the compiled and executed output is consistent with what is said in the book can you be considered to have learned an example. If it is inconsistent, you must carefully find the reason.

In addition to the examples in books, you also need to "create" some examples yourself. For example, when learning operator precedence, you can write several identical expressions and add parentheses in different positions to see what different behaviors there are. For example, *p++ and (*p)++, and a = b == c, (a = b) == c, a = (b == c), etc.

The examples in the book you copied, the examples after modification, and the examples you "created" should be carefully classified and saved, and a short comment should be written in the source code to explain the intention of the example.

After the examples are the exercises. I recommend that beginners do all the exercises independently, and then compare the answer codes to see what shortcomings their own code has, and then try to modify their code.

Many people do not pay attention to the exercises, which is a huge mistake, because the author usually explains some important principles in the exercises instead of simply testing the previous knowledge.

Maybe you think learning this way is too slow, but it’s not. If you study carefully, you don't have to look back. When you learn later and find out that you didn't understand it before, then the gain is really not worth the loss.

Generally speaking, after reading the entire book, you should complete thousands or even tens of thousands of lines of code. Whether you copy the book exactly or write it on your own whim, it will be an asset for you to continue learning in the future.

Take myself as an example. When I read "Windows Core Programming" (I only read 3/4 of the content), in addition to copying the code in the book, I also wrote many examples myself, with a total of 5574 lines (statistics using the wc tool under Unix ), after many days, I can no longer remember the system programming of Windows, but as long as I take a few minutes to dig out the previous code and look at it, I will understand it again. This is the saying that a good memory is worse than a bad writing.

Read carefully, carefully copy the source code, complete the exercises independently and conduct further experiments, and finally leave all the code behind to become your own experience and wealth. It is absolutely hard work, and it is definitely twice the result with half the effort. Of course, this method is only suitable for learning technologies that require proficiency. If you are not learning C language, you have to analyze the specific situation.

At the end of writing, there is still a very, very important point that was not mentioned - coding style. From the very beginning of learning, you must force yourself to imitate the best coding style.

                                                          ——EOF——

Guess you like

Origin blog.csdn.net/liuxing__jacker/article/details/132958962